Deep Insert in SAP Netweaver Gateway


Hello everyone, in this Netweaver Gateway tutorial we will learn how to use  Deep Insert 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 Deep Insert in SAP OData service does ?

Deep Insert is used to POST the nested structure of feed/collections to the back-end system.It is opposite to the $expand in which we GET the nested structure of collections. By implementing this you can reduce the no.of OData calls made to the SAP Netweaver Gateway server.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

Create a Sales Order header together with the items with in a single OData service call.

How to Implement Deep Insert in OData Service ?

The Deep Insert is implemented by using the method CREATE_DEEP_ENTITY.

Follow the below steps.

Prerequisite : You should create a OData service to support $expand which is explained in my earlier tutorials. Click here to access the tutorial.

1. After successful creation of OData service in SAP Netweaver Gateway system. Go to the DPC extension class and identity the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_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:    toordetitems   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,
          lt_itemdata        TYPE STANDARD TABLE OF  bapi_epm_so_item,
          ls_itemdata        TYPE bapi_epm_so_item,
          ls_req_itemdata    TYPE zcl_zdemo_gw_srv_mpc=>ts_orderitems,
          lt_return          TYPE STANDARD TABLE OF bapiret2,
          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 header and item data from the request to be created
        io_data_provider->read_entry_data( IMPORTING es_data = ls_order_item_data ).

*       Populate the BAPI structures
*       Sales Order Header
        ls_headerdata-buyer_id = ls_order_item_data-buyer_id.

*       Sales Order Item
        LOOP AT ls_order_item_data-toordetitems INTO ls_req_itemdata.
          MOVE-CORRESPONDING ls_req_itemdata TO ls_itemdata.
          GET TIME STAMP FIELD ls_itemdata-delivery_date.
          APPEND ls_itemdata TO lt_itemdata.

            headerdata   = ls_headerdata
            salesorderid = lv_so_id
            itemdata     = lt_itemdata
            return       = lt_return.
        IF sy-subrc = 0.
          CALL METHOD mo_context->get_message_container( )->add_messages_from_bapi(
              it_bapi_messages          = lt_return
              iv_determine_leading_msg  = /iwbep/if_message_container=>gcs_leading_msg_search_option-first ).
          RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
              textid            = /iwbep/cx_mgw_busi_exception=>business_error
              message_container = mo_context->get_message_container( ).

*       Push back the data again with newly created Sales Order ID
        CLEAR: ls_headerdata,lt_itemdata[],ls_order_item_data.
            so_id      = lv_so_id
            headerdata = ls_headerdata
            itemdata   = lt_itemdata.

        MOVE-CORRESPONDING ls_headerdata TO ls_order_item_data.
        APPEND LINES OF lt_itemdata TO ls_order_item_data-toordetitems.

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

            CALL METHOD super->/iwbep/if_mgw_appl_srv_runtime~create_deep_entity
                iv_entity_name          = iv_entity_name
                iv_entity_set_name      = iv_entity_set_name
                iv_source_name          = iv_source_name
                io_data_provider        = io_data_provider
                it_key_tab              = it_key_tab
                it_navigation_path      = it_navigation_path
                io_expand               = io_expand
                io_tech_request_context = io_tech_request_context
                er_deep_entity          = er_deep_entity.
          CATCH /iwbep/cx_mgw_busi_exception .
          CATCH /iwbep/cx_mgw_tech_exception .

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 use Deep Insert.
  2. Get the Sales Order Header and item data from the request using the method IO_DATA_PROVIDER->READ_ENTRY_DATA( ).
  3. Populate the BAPI structures with the data above retrieved.
  4. Call the BAPI BAPI_EPM_SO_CREATE to create the Sales Order with line items.
  5. If their are any errors messages in BAPI Return, send them to the OData service response and RAISE an EXCEPTION.
  6. If the BAPI ran successfully, get the Sales Order details using the BAPI BAPI_EPM_SO_GET_DETAIL and send them to the OData service response.

4. We are done with coding part. Lets test the service again in SAP Netweaver Gateway Client /IWFND/GW_CLIENT.Call the below OData service URI with HTTP GET, to get the data of any sales order using $expand you should see the below output.


$expand Implementation


5. Now press the button Use as Request, which exists in the response section in SAP Netweaver Gateway client. All the data from the response will be copied to the request section.

Use as Request

6. In the HTTP Request section clear the Sales order number and line item numbers, as we are creating a new sales order using this data.

Header and Item data


7. Now we are ready with data to send it to the SAP Netweaver Gateway server to create the sales order. Test the service by changing the HTTP Method to POST, and OData service URI to



8. New sales order will be created and data for the newly created sales order will be displayed in the HTTP Response section like below.

New Sales Order Created


You have successfully created sales order with nested structures using the Deep Insert concept tin SAP Netweaver Gateway.

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

Thank you.

  • apparao guthhikonda

    Nice Blog!!!
    Can you please tell me the links to access the Netweaver Gateway for Hands on .
    is it possible to install Demo version on PC/Laptop.

    Thanks for your help


      Hi Apparao,

      You can check this link SAP has provided a demo system to under SAP Netweaver Gateway and how to consume OData Services.

      Thank you.

      • Praveen

        Hi Ravikumar,

        Above mentioned system allow only read, it’s not allowing for creation for new project. if you have any link for practice system please guide me.


        • AdminSAPLearners

          Hi Praveen,

          That system is provided by SAP for READ ONLY mode.
          We also provide system access and ONLINE Training.
          If you are interested, please email to


  • kunal singh

    When creating the deep entity then there is a mismatch coming in the source and the target structure…you know where es_data(target structure) gets formed? due to this items not getting copied.