Technical Articles
Easy way to write data via a deep entity in OData
Introduction
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.
Solution
Please follow the steps:
- Tables involved: VBAK(Sales Document: Header Data), VBAP(Sales Document: Item Data).
- Create a Project in SEGW(SAP Gateway Service Builder)
- Import the DDIC Structure: VBAK(Sales Document: Header Data)
- Import the DDIC Structure: VBAP(Sales Document: Item Data)
- Create Association
- 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.
- Click on the Change(Ctrl+F1) button for editing.
- 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. *--------------------------------------------------------------------*
N.B:
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.
- 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
- 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 TRY. CALL METHOD io_data_provider->read_entry_data IMPORTING 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. ENDDO. TRY. 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 EXPORTING is_data = ls_deep_entity CHANGING cr_data = er_deep_entity. ENDIF. CATCH cx_root. "Error during insert ENDTRY. ENDIF. ENDIF. CATCH /iwbep/cx_mgw_tech_exception. "Do Nothing[ ENDTRY. ENDMETHOD.
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.
- 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.
- 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.
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.
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.
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!
Is there any way to bind multiple Deep Entity structures on different Entity within same SEGW project MPC_EXT-DEFINE method?
Thanks in advance.
I got an error when I processed the same steps as mentioned by you. Please help me to solve the below attach screenshot error.
Excellent blog!!!
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