SAP OData Association and Navigation

2
54234

Hello everyone, in this tutorial we will learn about SAP OData Association and Navigation concepts.

Association and Navigation

Association and Navigation are two important properties available in SAP OData Service to associate two entity types.

Let us understand this with an example, in our earlier tutorials we retrieved sales order header data and item data in two different entity types “SalesOrderSet” and “OrderItemsSet”.

OData Navigation and Assocation Step1

These two entity types are individual and can be executed differently.

But what we want here is based on selected Sales Order# we would like to pull the item data from other entity type.This is can be achieved using association and navigation property.

Lets look at that.

1. Go to Service Builder SEGW and expand the service to create an association

OData Navigation and Assocation Step2

2. Provide the following information and click on Next.

  • Principal Entity – To which you want to build the association
  • Dependent Entity – From which you want to get the data based on association
  • Cardinality – What the occurrence of no records
  • Navigation Property – Name of the navigation property for the Entity Type
OData Navigation and Assocation Step3

3. In the next screen, provide the common field among two entity sets and click on Next. In our case Sales Order Id is the common field.

OData Navigation and Assocation Step4

4. Check the entries and Click on Finish.

OData Navigation and Assocation Step5

5. This creates a navigation property for “SalesOrder” Entity Type and association property in OData service.

OData Navigation and Assocation Step6

6. Regenerate the service once again. You can observe that meta data of the service is grown because of our newly added association and navigation property.

To access the order items based on order# we just need to add the name of the navigation property of the Entity Type. If you try to execute the service now, you will run into error, we need to enhance the method GET_ENTITYSET of “Orderitems” entity type to get the data.

/sap/opu/odata/SAP/ZSL_EPM_DEMO_SRV/SalesOrderSet(‘500000000’)/ToOrderItems

OData Navigation and Assocation Step7

7. Open and enhance the code inside GET_ENTITYSET method of “Orderitems” entity type. Why only this method? because we called the URI with navigation property based on the cardinality of dependent entity methods we get triggered.

In our case our cardinality is 1..n of dependent entity i.e “Orderitems” so GET_ENTITYSET has to be enhanced.

8. If you go to that method you will find the code which we have already written for “Orderitems“, replace that code with the below code.

DATA:      ls_max_rows   TYPE bapi_epm_max_rows,
           lv_so_id      TYPE bapi_epm_so_id,
           lt_orderitems TYPE TABLE OF bapi_epm_so_item,
           ls_orderitems TYPE bapi_epm_so_item,
           lwa_key_tab   TYPE /iwbep/s_mgw_name_value_pair,
           ls_entityset  TYPE zcl_zdemo_gw_srv_mpc=>ts_orderitems.

* To get the Sales Order#
    READ TABLE it_key_tab INTO lwa_key_tab WITH KEY name = 'SoId'.
    IF sy-subrc = 0.
      lv_so_id = lwa_key_tab-value.
    ENDIF.

* if Sales Order is not available in the request,retrieve
* first 20 Sales Orders Items
    IF lv_so_id IS INITIAL.
      ls_max_rows-bapimaxrow = 20.
      CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
        EXPORTING
          max_rows   = ls_max_rows
        TABLES
          soitemdata = lt_orderitems.
      IF lt_orderitems IS NOT INITIAL.
        LOOP AT lt_orderitems INTO ls_orderitems.
          MOVE-CORRESPONDING ls_orderitems TO ls_entityset.
          APPEND ls_entityset TO et_entityset.
        ENDLOOP.
      ENDIF.
    ELSE.

* if Sales Order is available in the request,retrieve
* it's Order Items
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = lv_so_id
        IMPORTING
          output = lv_so_id.

      CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
        EXPORTING
          so_id    = lv_so_id
        TABLES
          itemdata = lt_orderitems.
      IF lt_orderitems IS NOT INITIAL.
        LOOP AT lt_orderitems INTO ls_orderitems.
          MOVE-CORRESPONDING ls_orderitems TO ls_entityset.
          APPEND ls_entityset TO et_entityset.
        ENDLOOP.
      ENDIF.
    ENDIF.

9.After activating the code. Try now the same URI to get the line items of a order, you should be able to see the items only specific to the order you have provided in the URI.

/sap/opu/odata/SAP/ZSL_EPM_DEMO_SRV/SalesOrderSet(‘500000000’)/ToOrderItems

OData Navigation and Assocation Step8
Association

Now you have successfully implemented the Association and Navigation in SAP Netweaver Gateway OData Service.

Conclusion

Stay tuned for is for more SAP Netweaver Gateway tutorials. Please feel free to comment and let us know your feedback.

Thank you.

Comments are closed.