SAP NetWeaver Gateway Serie: GET_ENTITYSET Method Implementation

SAP GateWay Serie GET ENTITYSET

In this serie dealing with SAP Netweaver Gateway Service, we will detail and give some useful and ready to use tips and ABAP sources to start with SAP oData service using SAP GW.

This article will detail the Query to retrieve oData Data Collection: GET_ENTITYSET.

What does the method GET_ENTITYSET do ?

Before starting with GET_ENTITYSET, you may want to check the following articles to have an overview on how SAP Netweaver Gateway works.

A GET_ENTITYSET method corresponds to a QUERY operation that returns zero to many entries. It is responsible for generating the entity collection, and is the equivalent of downloading and synchronizing MBOs.

Each entity has a corresponding service implementation that executes at runtime. You can implement standard operations such as create, read, update, and delete (CRUD) as well as query, to support the maintenance of the entity

The Signature of GET_ENTITYSET SAP Gateway Method

Here the signature of GET_ENTITYSET Method:

Attribute Type Description
IV_ENTITY_NAME STRING The Entity Name
IV_ENTITY_SET_NAME STRING The Entity Set Name
IV_SOURCE_NAME STRING The Entity Source ( if Navigation is used )
IT_FILTER_SELECT_OPTIONS /IWBEP/T_MGW_SELECT_OPTIONTable of select options
IS_PAGING /IWBEP/S_MGW_PAGINGPaging structure
IT_KEY_TAB /IWBEP/T_MGW_NAME_VALUE_PAIRTable for name value pairs
IT_NAVIGATION_PATH /IWBEP/T_MGW_NAVIGATION_PATHTable of navigation paths
IT_ORDER /IWBEP/T_MGW_SORTING_ORDERThe sorting order
IV_FILTER_STRING STRINGTable for name value pairs
IV_SEARCH_STRING STRING GW search String
IO_TECH_REQUEST_CONTEXT /IWBEP/IF_MGW_REQ_ENTITYSET All technical information
ET_ENTITYSET generated type from MPCReturning data
ES_RESPONSE_CONTEXT
/IWBEP/CX_MGW_BUSI_EXCEPTIONBusiness Exception in SAP GW mgw
/IWBEP/CX_MGW_TECH_EXCEPTIONmgw Technical Exception

Sample ABAP Implementation of GET_ENTITYSET in SAP GW

You find following some useful Implementations of GET_ENTITYSET SAP GW method for Business Partners, Sales Orders and Sales Order Items.
You will have a real cases for Navigation and Filter in SAP GW oData Service.

BUSINESSPARTNERS_GET_ENTITYSET

Here a sample of Implementing the BUSINESSPARTNERS_GET_ENTITYSET Method (source)

METHOD SALESORDERS_GET_ENTITYSET.

    FIELD-SYMBOLS: <fs_key> TYPE /iwbep/s_mgw_name_value_pair.
    DATA: ls_company_name TYPE snwd_company_name.
    DATA: lt_range     TYPE TABLE OF bapi_epm_customer_name_range,
          ls_range     TYPE bapi_epm_customer_name_range,
          lv_bp_id(10) TYPE n.

    " Return all SalesOrders if no navigation path.
    IF it_navigation_path IS INITIAL.
      CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
        TABLES
          soheaderdata = et_entityset .
    ELSE.
      " Navigation path from BusinessPartners. 
      " However, can only lookup using "Company Name", not BP_ID. 
      READ TABLE it_key_tab ASSIGNING <fs_key> INDEX 1.
      lv_bp_id = <fs_key>-value.

      " Look up buyer name based on BP_ID
      SELECT SINGLE company_name FROM snwd_bpa
      INTO ls_company_name WHERE bp_id = lv_bp_id.
      
      ls_range-sign = 'I'.
      ls_range-option = 'EQ'.
      ls_range-low = ls_company_name.
      APPEND ls_range TO lt_range.

      CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
        TABLES
          soheaderdata      = et_entityset
          selparambuyername = lt_range.
    ENDIF.
ENDMETHOD.

SALESORDERS_GET_ENTITYSET

Implement the GET_ENTITYSET method for the SalesOrders entity (source)

  METHOD salesorders_get_entityset.

    FIELD-SYMBOLS: <fs_key> TYPE /iwbep/s_mgw_name_value_pair.
    DATA: ls_company_name TYPE snwd_company_name.
    DATA: lt_range     TYPE TABLE OF bapi_epm_customer_name_range,
          ls_range     TYPE bapi_epm_customer_name_range,
          lv_bp_id(10) TYPE n.

    " Return all SalesOrders if no navigation path.
    IF it_navigation_path IS INITIAL.
      CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
        TABLES
          soheaderdata = et_entityset.
    ELSE.

      READ TABLE it_key_tab ASSIGNING <fs_key> INDEX 1.
      lv_bp_id = <fs_key>-value.

      SELECT SINGLE company_name FROM snwd_bpa
      INTO ls_company_name  WHERE bp_id = lv_bp_id.

      ls_range-sign = 'I'.
      ls_range-option = 'EQ'.
      ls_range-low = ls_company_name.
      APPEND ls_range TO lt_range.

      CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
        TABLES
          soheaderdata      = et_entityset
          selparambuyername = lt_range.

    ENDIF.

  ENDMETHOD.

SALESORDERITEMS_GET_ENTITYSET

Implement the GET_ENTITYSET method for the SalesOrderItems entity. (source)

  METHOD salesorderitems_get_entityset.

    DATA: lt_range     TYPE TABLE OF bapi_epm_so_id_range,
          ls_range     TYPE bapi_epm_so_id_range,
          lv_so_id(10) TYPE n.
    FIELD-SYMBOLS: <fs_key> TYPE /iwbep/s_mgw_name_value_pair.
    
    " limit access to SalesOrderItmes through a navigation path or $expand option.
    IF it_navigation_path IS INITIAL.
      " If it's not, raise an exception.
      RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
        EXPORTING
          textid            = /iwbep/cx_mgw_busi_exception=>business_error_unlimited
          message_unlimited = 'SalesOrderItems can only be accessed from a sales' && 
                          'Order navigation path or using the $expand=items option.'.
    ELSE.
      "  There is a navigation path. Read only items for a particular SO
      "  SO ID should be in it_key_tab.
      READ TABLE it_key_tab ASSIGNING <fs_key> INDEX 1.
      lv_so_id = <fs_key>-value.
      ls_range-sign = 'I'.
      ls_range-option = 'EQ'.
      ls_range-low = lv_so_id.
      APPEND ls_range TO lt_range.

      CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
        TABLES
          soitemdata   = et_entityset
          selparamsoid = lt_range.
    ENDIF.
  ENDMETHOD.

Going Further with SAP Gateway Odata Service