Wednesday 6 April 2022

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:

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

2. Create a Project in SEGW(SAP Gateway Service Builder).

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

3. Import the DDIC Structure: VBAK(Sales Document: Header Data).

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

4. Import the DDIC Structure: VBAP(Sales Document: Item Data)

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

5. Create Association

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

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.

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

7. Click on the Change(Ctrl+F1) button for editing.
8. Click on the Direct Type Entry button. Then, create the deep structure & activate.

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

*--------------------------------------------------------------------*
* 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.

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

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 Alias name(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:

NW ABAP Gateway (OData), ABAP Connectivity, ABAP Extensibility, ABAP RESTful Application Programming Model, OData, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs

Source: sap.com

No comments:

Post a Comment