Self-Expand Implementation
$expand is a query option that is used to retrieve several entities that are connected via navigation properties in one request or response cycle.
When using $expand, you actually follow the navigation properties of an OData service.
In our example, $expand is used to either retrieve all sales orders of a business partner or all items of a sales order. Alternatively, it can be used, for example, to retrieve all sales orders including all their items of a business partner.
OData Service requirements include the following:
Note: No manual coding is required.
Examples of $expand
The following are examples without $expand (5 calls):
The following are examples with $expand (1 call):
$expand Generic Framework Support
When using the generic framework support for $expand, the following methods are called in the service implementation:
When using self-expand, you must implement the following method in the data provider extension class: /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITY
Here optimized coding can be implemented that is faster than calling the different single GET_ENTITY and GET_ENTITYSET methods.
$expand Complete Entity Set
The framework also supports the $expand of a complete entity set.
When using self-expand for a complete entity set, you must implement the following method in the data provider extension class:
/IWBEP/IF_MGW_APPL_SRV
$expand GET_EXPANDED_ENTITY Code
The coding starts with the definition of deep structures that return the expanded line items of a sales order and the expanded sales orders with their line items of a business partner.
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entity.
IF lv_entityset_name EQ 'BusinessPartnerSet' AND
io_expand->compare_to_tech_names( 'SALESORDERS/LINEITEMS' ) EQ
io_expand->gcs_compare_result-match_equals.
* get all sales order headers and all sales order items in one call
CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
TABLES
soheaderdata = lt_so_headerdata
soitemdata = lt_so_itemdata
selparambuyername = lt_buyer_name
return = lt_return.
LOOP AT lt_so_itemdata INTO ls_so_itemdata.
AT NEW so_id.
* Append lines items of the current sales order
* Append the sales order header data
ENDAT.
copy_data_to_ref(
EXPORTING
is_data = ls_bp_so_lineitems
CHANGING
cr_data = er_entity
ls_expanded_clause = 'SALESORDERS/LINEITEMS'.
APPEND ls_expanded_clause TO et_expanded_tech_clauses.
ELSE.
* Call framework
ENDIF.
ENDMETHOD.
We then check whether expand string $expand=SalesOrders/LineItems is used.
Since the BAPI BAPI_EPM_SO_GET_LIST only offers the business partner name as a select option, you have to determine the name of the business partner first. The returned header data lt_so_headerdata and item data lt_so_itemdata are first sorted before filling the deep structure.
Use the AT NEW statement to check for a new sales order ID (SO_ID).
At this point, the corresponding Sales Order Items and the Sales Order Header data are added to the deep structure.
Finally, use the method copy_data_to_ref since the method we are implementing expects a reference.