Skip to Content
Technical Articles
Author's profile photo Kallol Chakraborty

Easy way to write data via a deep entity in OData


I have seen many tutorials based on OData but I really found them complex for beginners.

So, I am writing this blog post for quick easy reference. This is the 2nd blog post in the series.

For the first post please check the Easy way to read data via a deep entity in OData.


Please follow the steps:

  1. Tables involved: VBAK(Sales Document: Header Data), VBAP(Sales Document: Item Data).
  2. Create a Project in SEGW(SAP Gateway Service Builder)
  3. Import the DDIC Structure: VBAK(Sales Document: Header Data)
  4. Import the DDIC Structure: VBAP(Sales Document: Item Data)
  5. Create Association
  6. Go to Runtime Artifacts node, open the ZCL_ZGW_PRACTICE006_MPC_EXT class in ABAP Workbench(Right-Click: Go to ABAP Workbench) & click on the Types tab.
  7. Click on the Change(Ctrl+F1) button for editing.
  8. Click on the Direct Type Entry button. Then, create the deep structure & activate.
    * Deep Structure
        TYPES: BEGIN OF ty_deep_entity,
                 vbeln        TYPE vbeln_va,    "Sales Document
                 erdat        TYPE erdat,       "Date on which the record was created
                 erzet        TYPE erzet,       "Entry time
                 ernam        TYPE ernam,       "Name of Person who Created the Object
                 vkorg        TYPE vkorg,       "Sales Organization
    * Navigation property name should be used otherwise empty records will be shown
                 headertoitem TYPE TABLE OF ts_sales_item_data WITH DEFAULT KEY,
               END OF ty_deep_entity.


    The navigation property name should be used in case of a deep entity like shown in the image above otherwise, empty records will be returned.

    Do not regenerate the service before taking the backup as it will delete all the custom structures.

    Just redefine the basic methods: *GET_ENTITY & *GET_ENTITYSET of the entities for easy troubleshooting. No need to write any code within the methods. The $expand keyword will call only the GET_EXPANDED_ENTITYSET method.

  9. Redefine the DEFINE method of the ZCL_ZGW_PRACTICE006_MPC_EXT and enter the below code.
    DATA lo_entity_type TYPE REF TO /iwbep/if_mgw_odata_entity_typ.
        super->define( ).
    * Header Entity Name
        lo_entity_type = model->get_entity_type( iv_entity_name = 'Sales_Header_Data' ).
    * MPC_EXT Deep Structure Name
        lo_entity_type->bind_structure( iv_structure_name = 'ZCL_ZGW_PRACTICE006_MPC_EXT=>TY_DEEP_ENTITY​
  10. Go to the ZCL_ZGW_PRACTICE006_DPC_EXT class & redefine the method: CREATE_DEEP_ENTITY.
     METHOD /iwbep/if_mgw_appl_srv_runtime~create_deep_entity.
        DATA : ls_deep_entity TYPE zcl_zgw_practice006_mpc_ext=>ty_deep_entity,
               lt_item        TYPE TABLE OF zcl_zgw_practice006_mpc_ext=>ts_sales_item_data,
               ls_header      TYPE zcl_zgw_practice006_mpc_ext=>ts_sales_header_data.
    * Methods/FMs should be used in case of direct database table update
    * Reading the entity data through the parameter: io_data_provider
            CALL METHOD io_data_provider->read_entry_data
                es_data = ls_deep_entity.
            IF ls_deep_entity IS NOT INITIAL.
              ls_header = CORRESPONDING #( ls_deep_entity ).
              ls_header-mandt = sy-mandt.
              MODIFY vbak FROM ls_header.
              IF sy-subrc = 0.
                lt_item[] = CORRESPONDING #( ls_deep_entity-headertoitem[] ).
                DO lines( lt_item[] ) TIMES.
                  lt_item[ sy-index ]-mandt = sy-mandt.
                    INSERT vbap FROM TABLE lt_item ACCEPTING DUPLICATE KEYS.
                    IF sy-subrc = 4.                     "To overcome the dump
                      CALL METHOD me->copy_data_to_ref   "Populating the ER_DEEP_ENTITY
                          is_data = ls_deep_entity
                          cr_data = er_deep_entity.
                  CATCH cx_root.
                    "Error during insert
          CATCH /iwbep/cx_mgw_tech_exception.
            "Do Nothing[

    N.B: Here, the OPEN SQL statement is used to insert/update entries in the database tables. It’s better to use FMs/Methods for the same.

  11. Input: Go to the HTTP Request section then enter your JSON/XML file & select the radio button POST & execute.
      "Vbeln" : "0000032183",
      "Erdat" : "\/Date(1481760000000)\/",
      "Erzet" : "TEST0000001",
      "Ernam" : "Kallol",
      "Vkorg" : "1710",
      "HeaderToItem" : [
            "Vbeln" : "0000032183",
            "Posnr" : "000010",
            "Matnr" : "MZ-FG-M550",
            "Arktx" : "M550 BIKE",
            "Posar" : ""
            "Vbeln" : "0000032183",
            "Posnr" : "000020",
            "Matnr" : "LMO-PRD-M121",
            "Arktx" : "1ortable DVD Player PDP-121",
            "Posar" : ""
            "Vbeln" : "0000032183",
            "Posnr" : "000030",
            "Matnr" : "MZ-FG-C950",
            "Arktx" : "C950 BIKE",
            "Posar" : ""

    N.B: Remove the ?$expand=HeaderToItem&$format=json from the URI otherwise it will throw error while inserting the data.

  12. Output

That’s it. 🙂

N.B: This is not a proper way to update the database tables. Please use FMs/Methods for the same otherwise there will be discrepancies. This blog post is only for showing how the insert happens via a deep entity. You can manipulate the code according to your requirements.

If I have missed something, please feel free to add it in the comment section so that, this post can be useful to others.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.