$expand Query Option in SAP OData Service

0
9472

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

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.

Supported System Version

SAP NetWeaver Gateway Release 2.0 Support Package >=03

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 sales order and second is to get order items for that sales order, but we want them in a single service call. To achieve this $expand query option is used.

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 ?

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.

$expand Query Option

 

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

  1. First, it calls the GET_ENTITY / GET_ENTITYSET for Sales Order headers.
  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, 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.