Technical Articles
Easy way to read 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.
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.
- Go to the ZCL_ZGW_PRACTICE006_DPC_EXT class & redefine the method: GET_EXPANDED_ENTITYSET.
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset. DATA : lt_deep_entity TYPE TABLE OF zcl_zgw_practice006_mpc_ext=>ty_deep_entity, "Deep Entity Type ls_deep_entity TYPE zcl_zgw_practice006_mpc_ext=>ty_deep_entity, "Deep Entity Type ls_item TYPE zcl_zgw_practice006_mpc_ext=>ts_sales_item_data. * Based on the entity set IF iv_entity_set_name = 'Sales_Header_DataSet'. SELECT * FROM vbak INTO TABLE @DATA(lt_vbak) * UP TO 20 ROWS. WHERE vbeln = '0000018338'. "According to the requirements IF sy-subrc = 0. SELECT * FROM vbap INTO TABLE @DATA(lt_vbap) FOR ALL ENTRIES IN @lt_vbak WHERE vbeln = @lt_vbak-vbeln. ENDIF. IF lt_vbak IS NOT INITIAL AND lt_vbak IS NOT INITIAL. SORT lt_vbak DESCENDING BY vbeln. SORT lt_vbap DESCENDING BY vbeln. LOOP AT lt_vbak INTO DATA(ls_vbak_item). ls_deep_entity = CORRESPONDING #( ls_vbak_item ). IF line_exists( lt_vbap[ vbeln = ls_deep_entity-vbeln ] ). LOOP AT lt_vbap INTO DATA(ls_vbap_item) FROM sy-tabix. IF ls_vbak_item-vbeln <> ls_vbap_item-vbeln. EXIT. ENDIF. ls_item = CORRESPONDING #( ls_vbap_item ). * Appending to the deep structure APPEND ls_item TO ls_deep_entity-headertoitem. CLEAR ls_item. ENDLOOP. APPEND ls_deep_entity TO lt_deep_entity. ENDIF. CLEAR ls_vbak_item. ENDLOOP. ENDIF. * For converting the data in the deep structure to the output format CALL METHOD me->/iwbep/if_mgw_conv_srv_runtime~copy_data_to_ref EXPORTING is_data = lt_deep_entity CHANGING cr_data = er_entityset. ENDIF. ENDMETHOD.
- Go to the TCODE: /N/IWFND/MAINT_SERVICE & REGISTER the service by providing the service name & the Alias name(Central Hub Only).
- Go to the TCODE: /N/IWFND/GW_CLIENT for testing the service. Enter the below URI in the Request URI section.
/sap/opu/odata/sap/ZGW_PRACTICE006_SRV/Sales_Header_DataSet?$expand=HeaderToItem&$format=json
- Output:
That’s it. 🙂
If I have missed something, please feel free to add it in the comment section so that, this post can be useful to others.
The easy way since 7.5 is with the VDM using @OData.Publish=true. As of S/4 2020 even that has been superseded by the Restful ABAP model.
See online help: Developing New Transactional Apps
If you really need something more manual then go for Referenced Data Sources
But programming services directly is usually the last option to consider when you have requirements that cannot be fulfilled using CDS models.
The big advantage that the VDM offers is extensibility, create a service once and extend it for other purposes. Use CDS in your ABAP reports or build Fiori Elements apps on top of it in a matter of hours.
Thanks, Mike Pokraka for reviewing the post.
Thanks for mentioning the VDM. I hope the viewers will see your comment and check the VDM approach.
This is not a HANA approach, CDS is available on AnyDB since 7.50.
Hi Kallol Chakraborty,
is it Not required to Redefine the "DEFINE" method from MPC Extension Class and bind the deep Structure?
because in Create Deep Entity blog(Easy way to write data via a deep entity in OData) you are following the Redefining the "DEFINE" method in MPC Extension Class ?
Regards,
B@LU.