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

      You must be Logged on to comment or reply to a post.
      Author's profile photo Swamy swamy
      Swamy swamy

      HI Kallol Chakraborty,

      we are  creatingl create deep entity with ref to your post we are getting: 'csrf token validation failed' message how to overcome this.

      we did not Implement get_expanded_Deep_entity as it is not needed, please let me know any solution to call creat Deep entity for testing.

      Author's profile photo Bhaskar Chakraborty
      Bhaskar Chakraborty

      Just came accross your question today, probably bit too late for you.

      But to use Create Deep Entity you first need to call the Get Method of the root entity and pass the below parameter in Request Header.

      x-csrf-token = fetch

      You will get the value of xcrsf token in Response header. Then call the create deep entity passing the xcsrf token in the request header which you earlier received. This should solve your problem.

      x-csrf-token = fnZX7zSr3JBG4oAB1uYrUg==  => the value is just for egxample. This will mostprobably be valid for 30 min depending on your system config.

      Let me know if its useful for you.

      Author's profile photo Eduardo Ribeiro
      Eduardo Ribeiro

      A few years ago, if I updated the sales order tables like that, I would get fired.

      The world is changing. Indeed.

      Hey! I think I'll try to earn money playing video games and chatting!

      Thanks for teaching, friend!

      Author's profile photo Rohit Singh
      Rohit Singh

      Is there any way to bind multiple Deep Entity structures on different Entity within same SEGW project MPC_EXT-DEFINE method?


      Thanks in advance.

      Author's profile photo Rahul Mali
      Rahul Mali

      I got an error when I processed the same steps as mentioned by you. Please help me to solve the below attach screenshot error.


      Author's profile photo kyo choi
      kyo choi

      Excellent blog!!!

      Author's profile photo kapil mahire
      kapil mahire

      I followed all the above steps but thing is that when i Redefine the DEFINE method of the ZCL_ZGW_PRACTICE006_MPC_EXT  then my entityset data is not visible @Gateway client.

      Please suggest