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
1.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.
2.Supported System Version
SAP NetWeaver Gateway Release-采购向厂商下P/O之后,必须透过一定权限经由主管对此张P/O做 release后才能收料 2.0 Support Package >=03
3.Business Example
Create a Sales Order header together with the items with in a single OData service call.
4.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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
DATA : BEGIN OF ls_order_item_data. INCLUDE TYPE zcl_zdemo_gw_srv_mpc=>ts_sale-ale settingssorder. 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. ENDLOOP. CALL FUNCTION 'BAPI_EPM_SO_CREATE' EXPORTING headerdata = ls_headerdata IMPORTING sale-ale settingssorderid = lv_so_id TABLES itemdata = lt_itemdata return = lt_return. READ TABLE lt_return TRANSPORTING NO FIELDS WITH KEY type = 'E'. IF sy-subrc = 0. CALL METHOD mo_context->get_message_container( )->add_messages_from_bapi( EXPORTING 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 EXPORTING textid = /iwbep/cx_mgw_busi_exception=>business_error message_container = mo_context->get_message_container( ). ENDIF. * Push back the data again with newly created Sales Order ID CLEAR: ls_headerdata,lt_itemdata[],ls_order_item_data. CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL' EXPORTING so_id = lv_so_id IMPORTING headerdata = ls_headerdata TABLES 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 ). WHEN OTHERS. TRY. CALL METHOD super->/iwbep/if_mgw_appl_srv_runtime~create_deep_entity EXPORTING 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 IMPORTING er_deep_entity = er_deep_entity. 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
Check the Entity set name is same as the Entity set name for which we need to use Deep Insert.
Get the Sales Order Header and item data from the request using the method IO_DATA_PROVIDER->READ_ENTRY_DATA( ).
Populate the BAPI structures with the data above retrieved.
Call the BAPI BAPI_EPM_SO_CREATE to create the Sales Order with line items.
If their are any errors messages in BAPI Return, send them to the OData service response and RAISE an EXCEPTION.
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 sale-ale settingss order using $expand you should see the below output.
/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/SalesOrderSet(‘500000017’)?$expand=ToOrdetItems
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.
6. In the HTTP Request section clear the Sales order number and line item numbers, as we are creating a new sale-ale settingss order using this data.
7. Now we are ready with data to send it to the SAP Netweaver Gateway server to create the sale-ale settingss order. Test the service by changing the HTTP Method to POST, and OData service URI to
/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/SalesOrderSet
8. New sale-ale settingss order will be created and data for the newly created sale-ale settingss order will be displayed in the HTTP Response section like below.
You have successfully created sale-ale settingss 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.