Easy way to read data via a deep entity in OData (2024)

Table of Contents
Introduction Solution

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:


  1. Tables involved: VBAK(Sales Document: Header Data), VBAP(Sales Document: Item Data).

  2. Create a Project in SEGW(SAP Gateway Service Builder). Easy way to read data via a deep entity in OData (1)

  3. Import the DDIC Structure: VBAK(Sales Document: Header Data).Easy way to read data via a deep entity in OData (2)Easy way to read data via a deep entity in OData (3)Easy way to read data via a deep entity in OData (4)

  4. Import the DDIC Structure: VBAP(Sales Document: Item Data)Easy way to read data via a deep entity in OData (5)Easy way to read data via a deep entity in OData (6)Easy way to read data via a deep entity in OData (7)

  5. Create AssociationEasy way to read data via a deep entity in OData (8)Easy way to read data via a deep entity in OData (9)Easy way to read data via a deep entity in OData (10)

  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.Easy way to read data via a deep entity in OData (11)

  7. Click on the Change(Ctrl+F1) button for editing.

  8. Click on the Direct Type Entry button. Then, create the deep structure & activate.Easy way to read data via a deep entity in OData (12)
    *--------------------------------------------------------------------*
    * 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.


  9. Go to the ZCL_ZGW_PRACTICE006_DPC_EXT class & redefine the method: GET_EXPANDED_ENTITYSET.Easy way to read data via a deep entity in OData (13)
     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.​



  10. Go to the TCODE: /N/IWFND/MAINT_SERVICE & REGISTER the service by providing the service name & the Aliasname(Central Hub Only).

  11. 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​


  12. Output:Easy way to read data via a deep entity in OData (14)

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.

Easy way to read data via a deep entity in OData (2024)
Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 6055

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.