Association and Navigation in OData

In this blog we will be covering the concept of association and navigation in OData. 

In this tutorial, we will take example of navigating from one Sales Order to the related Sales Order line items, by using a link instead of manually putting a filter together. Moreover, it also allows us to use the $expand statement to fetch the Sales Order together will all the Sales Order line items in one call.

Lets say, we already have created an OData service in SEGW named ZGW100_XX_SO. Double click on Associations.
Press Create button.

Now, Enter the following values for association and press enter:
   

Name

SalesOrderSalesOrderItems

Principal Entity*

SalesOrder

Principal Entity Cardinality*

1

Dependent Entity*

SalesOrderItem

Dependent Entity Cardinality*

M


Now, create a referential constraint for the association
1) Expand the Associations node and the SalesOrderSalesOrderItems node and double-click Referential Constraints:















2) Choose the Create pushbutton:






3) Enter the following values and choose Enter:

Principal Key*

SoId

Dependent Property*

SoId

* This field has an Input Help




Now we create an association set for the association

1. Double-click Association Sets:


2. Choose the Create pushbutton:


3. Enter the values and choose Enter:

Name

SalesOrderSalesOrderItems

Association*

SalesOrderSalesOrderItems


And finally we create a navigation property based on the referential constraint

1. Expand Data Model > Entity Types > SalesOrder and double-click Navigation Properties:


2. Choose the Create pushbutton:

3. Enter the following values and choose Enter:

Name

SalesOrderItems

Relationship Name*

SalesOrderSalesOrderItems

Now we need to re-generate the runtime objects and we’re then ready to test the service

1. Choose the Generate pushbutton:

2. Verify that the runtime objects have been generated successfully:

3. Start the Gateway Client (Transaction /IWFND/GW_CLIENT) in a separate window to run the service. Provide the following URI to get the metadata for the service: 

/sap/opu/odata/sap/ZGW100_XX_SO_SRV/$metadata


The Sales Order collection now includes a navigation property.

4. When you now select a sales order entry using

/sap/opu/odata/sap/ZGW100_XX_SO_SRV/SalesOrderCollection(‘0500000001’), for example, you can simply add the navigation link /SalesOrderItems to navigate to the line items without having to set a filter yourself:


5. And finally you can use $expand to read all sales order items for a given sales order in a single http call.

Simply provide the following URI:

/sap/opu/odata/sap/ZGW100_XX_SO_SRV/SalesOrderCollection(‘0500000001’)/?$expand=SalesOrderItems


The $expand statement is handled by the framework (no additional implementation is required). Since the framework does not know that both entities can be obtained using a single RFC call, it executes two calls to the underlying BAPI. This can be improved by manually implementing (re-defining) the GET_EXPANDED_ENTITY method.

So we are done. The Service is up and running.


























Previous
Next Post »