首页 > SAP > ODATA > $expand Query Option in SAP OData Service
2019
08-29

$expand Query Option in SAP OData Service

[adsenseyu2]

Hello everyone, in this Netweaver Gateway tutorial we will learn how to use  $expand query option in SAP netweaver gateway OData service. Before proceeding further we assume that you know how to build OData service in sap gateway. Access all SAP Netweaver Gateway tutorials here.Lets get started

1.What $expand query option does ?

Query option $expand is used to read multiple entities or entity sets in a single service call instead of two different calls. Prerequisite, entity sets which are used should be associated. To know about Association in OData service click here.

2.Supported System Version

SAP NetWeaver Gateway Release-采购向厂商下P/O之后,必须透过一定权限经由主管对此张P/O做 release后才能收料 2.0 Support Package >=03

3.Business Example

A gateway OData service is having two entity sets ‘SalesOrderSet‘ and ‘OrderItemsSet‘ which are associated. Usually to get the Order and its item details we require two service calls one for sale-ale settingss order and second is to get order items for that sale-ale settingss order, but we want them in a single service call. To achieve this $expand query option is used.

4.Syntax

Single Sales Order and its line items
http://<server>:<port>/sap/opu/odata/sap/<servicename>/SalesOrders(‘123’)?$expand=ToOrderItems

Multiple Sales Order and their line items
http://<server>:<port>/sap/opu/odata/sap/<servicename>/SalesOrders?$expand=ToOrderItems

where $expand = <Navigation_Property_Name>

5.How to Implement $expand query option in OData Service ?

Normally no implementation is required explicitly to use $expand query option. By default SAP NetWeaver Gateway provided a generic $expand implementation.

We can directly test the service by adding $expand query option, so lets do that and see whether we are able get the order and line item data in a single call. We assume that you have already build the OData service with two entity sets to get the orders and line items. if you have not click here.

Execute the below URI in SAP Netweaver Gateway Client.
/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/SalesOrderSet(‘500000017’)?$expand=ToOrdetItems.

You should get both Order Information and Item information in single service call.Output below.

6.Now the question is , Why should we need to do an explicit implementation for $expand ?

SAP Netweaver Gateway will handle the $expand query function in this way

First, it calls the GET_ENTITY / GET_ENTITYSET for Sales Order headers.
Second, it calls the GET_ENTITYSET of Order Items in a Loop for each above Sales Orders retrieved.

This make the standard framework a performance issue, so to boost-up the performance we do an explicit implementation for $expand query option by using following methods

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITY

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET.

Follow the below steps to do the $expand query option implementation.

1. After successful creation of OData service. Go to the DPC_EXT class and identity the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITY and redefine it.

2. Copy and paste the below code in the method.

3. Let us see what we have done in the above step

Check the Entity set name is same as the Entity set name for which we need to $expand implementation.
Get the Sales Order No and Call BAPI to get both header and item data.
Set back the data in the response.

4. We are done with coding part. Lets test the service again, we should get the same output as the standard $expand output earlier.

The outputs are same, so you have successfully implemented the $expand in SAP Netweaver Gateway OData service.

Stay tuned to us for more SAP Netweaver Gateway tutorials.Please feel free to comment and let us know your feedback.

Thank you.

最后编辑:
作者:yangsen
有需要可在文章留言或者邮件yan252@163.com给我。