$expand Query Option in SAP OData Service

0
40187

Dear SAPLearners, in this gateway tutorial we will learn how to use  $expand query option in SAP OData service.

$expand Query Option

OData 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.

Also Read: Frequently used OData query options

Supported System Version

SAP NetWeaver Gateway Release 2.0 Support Package >=03

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>

How to Implement $expand query option in OData Service?

Explicit implementation is not required to use $expand query option. By default SAP OData Gateway framework provides a generic $expand implementation.We can directly test the service by adding $expand query option

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 built the OData service with two entity sets and these you have established the association between to get the orders and line items. if you have not click here.

Also Read: Association and Navigation in SAP OData Service

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

You will get both order header and item data in single service call. Sample output below

$expand Query Option

 

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

SAP OData framework will handle the $expand query function in this way

  1. First, it calls the GET_ENTITY / GET_ENTITYSET for sales order header data.
  2. 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.

    DATA : BEGIN OF ls_order_item_data.
            INCLUDE         TYPE zcl_zdemo_gw_srv_mpc=>ts_salesorder.
    DATA:    itemdata       TYPE zcl_zdemo_gw_srv_mpc=>tt_orderitems,
             END OF ls_order_item_data.

    DATA: lv_entity_set_name TYPE /iwbep/mgw_tech_name,
          ls_headerdata      TYPE bapi_epm_so_header,
          ls_key_tab         TYPE /iwbep/s_mgw_name_value_pair,
          lt_itemdata        TYPE STANDARD TABLE OF  bapi_epm_so_item,
          ls_itemdata        TYPE bapi_epm_so_item,
          ls_res_itemdata    TYPE zcl_zdemo_gw_srv_mpc=>ts_orderitems,
          lv_so_id           TYPE bapi_epm_so_id.

* Get Entity Set Name
    lv_entity_set_name     = io_tech_request_context->get_entity_set_name( ).

    CASE lv_entity_set_name.
      WHEN 'SalesOrderSet'.

*    Get the Sales Order#
        READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'SoId'.
        IF sy-subrc = 0.
          lv_so_id = ls_key_tab-value.
        ENDIF.
        
*    Get Order header info and item info
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
          EXPORTING
            input  = lv_so_id
          IMPORTING
            output = lv_so_id.

        CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
          EXPORTING
            so_id      = lv_so_id
          IMPORTING
            headerdata = ls_headerdata
          TABLES
            itemdata   = lt_itemdata.
        
*    Set both header and item data to the response
        MOVE-CORRESPONDING ls_headerdata TO ls_order_item_data.
        APPEND LINES OF lt_itemdata TO ls_order_item_data-itemdata.

        copy_data_to_ref( EXPORTING is_data = ls_order_item_data
                          CHANGING  cr_data = er_entity ).

      WHEN OTHERS.
        TRY.
            CALL METHOD super->/iwbep/if_mgw_appl_srv_runtime~get_expanded_entity
              EXPORTING
                iv_entity_name           = iv_entity_name
                iv_entity_set_name       = iv_entity_set_name
                iv_source_name           = iv_source_name
                it_key_tab               = it_key_tab
                it_navigation_path       = it_navigation_path
                io_expand                = io_expand
                io_tech_request_context  = io_tech_request_context
              IMPORTING
                er_entity                = er_entity
                es_response_context      = es_response_context
                et_expanded_clauses      = et_expanded_clauses
                et_expanded_tech_clauses = et_expanded_tech_clauses.
          CATCH /iwbep/cx_mgw_busi_exception .
          CATCH /iwbep/cx_mgw_tech_exception .
        ENDTRY.
    ENDCASE.

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

  1. Check the Entity set name is same as the Entity set name for which we need to $expand implementation.
  2. Get the Sales Order No and Call BAPI to get both header and item data.
  3. 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.

$expand Implementation

The outputs are same.

Congrats!! you have successfully implemented the $expand in SAP Netweaver Gateway OData service. Stay tuned to us for more SAP OData Gateway tutorials.

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!