Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
kmabhilash111
Employee
Employee
Hello,

Writing this blog for purpose of beginners in order to explain Deep structures handling in SAP OData.

Usually we encounter a business case where in we need to Fetch Parent child relationship data in single call  or we may need to save the Header Item details to database.

OData framework provides an option to perform this operations using Deep Structures.

Agenda : Simple example to depict Fetch/Get scenario.

Procedure :

Let us consider case of Header to Item (1 to 1)

Header Table:

Item Table :

Relation between header and item -> ID 

Create a project in SEGW and import these 2 structures into the project

Create an association and navigation for Header and Item

Now for the purpose of Deep structures, we will create a SE11 structure similar to this navigation property.

Note: The Item structure name should be similar to Navigation property.

Note : The structure for this purpose can also be created in MPC_EXT public section, I have created in SE11 for demonstrating the other possibilities.



Now, we are set with declarations and we can start the coding part.

Go to DPC_EXT class after generating the services and redefine the /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET.

Expanded entityset will serve to expand entire header set to item, in case of single header and related item expansion we will have to redefine Expanded entity. we will discuss it in later part.


  METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.

DATA : it_out_tab TYPE STANDARD TABLE OF zdeep_s,
wa_out_tab LIKE LINE OF it_out_tab,

it_header TYPE STANDARD TABLE OF zheader,
wa_header TYPE zheader,

it_item TYPE STANDARD TABLE OF zitem,
wa_item TYPE zitem.

CASE iv_entity_set_name.

WHEN 'HeaderSet'.
SELECT * FROM zheader INTO TABLE it_header.
IF sy-subrc = 0.
SELECT * FROM zitem INTO TABLE it_item FOR ALL ENTRIES IN it_header WHERE id = it_header-id.
ENDIF.


LOOP AT it_header INTO wa_header.
MOVE-CORRESPONDING wa_header TO wa_out_tab.
LOOP AT it_item INTO wa_item WHERE id = wa_header-id.
APPEND wa_item TO wa_out_tab-headertoitemnav.
CLEAR : wa_item.
ENDLOOP.
APPEND wa_out_tab TO it_out_tab.
CLEAR : wa_header,wa_out_tab.
ENDLOOP.

copy_data_to_ref(
EXPORTING
is_data = it_out_tab
CHANGING
cr_data = er_entityset ).

ENDCASE.

ENDMETHOD.

I have written sample code to retrieve the data, this code is not optimized and for demo purpose.Change the code as per the standards if necessary.

We are now done with coding part, we can test the service by using the following URL.

URI : /sap/opu/odata/sap/ZDEEPSTRUCTURES_SRV/HeaderSet?$expand=HeaderToItemNav

Entries in Header TableEntries in Item Table

 

Below is the output after executing the URI
{
"d" : {
"results" : [
{
"__metadata" : {
},
"Id" : "100",
"Name" : "MOUSE",
"Dept" : "DEPT1",
"HeaderToItemNav" : {
"results" : [
{
"__metadata" : {
},
"Id" : "100",
"Sdept" : "DEPT-1A",
"Stock" : "22"
},
{
"__metadata" : {
},
"Id" : "100",
"Sdept" : "DEPT-1B",
"Stock" : "44"
},
{
"__metadata" : {
},
"Id" : "100",
"Sdept" : "DEPT-1C",
"Stock" : "76"
}
]
}
},
{
"__metadata" : {
},
"Id" : "101",
"Name" : "KEYBOARD",
"Dept" : "DEPT2",
"HeaderToItemNav" : {
"results" : [
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT2-SM",
"Stock" : "115"
},
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT-2C",
"Stock" : "129"
},
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT-2D",
"Stock" : "251"
},
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT-2M",
"Stock" : "009"
}
]
}
}
]
}
}

 

We can do the similar kind of code for Expand entity for expanding single header and relative Item set. Difference would be to add the key in header set.

URI : /sap/opu/odata/sap/ZDEEPSTRUCTURES_SRV/HeaderSet('100')?$expand=HeaderToItemNav

Conclusion: The same can also be achieved from just redefining Get entity of Header Set and Get entity set of Item Set. But, we do this kind to reduce the time of looping though these methods. on the other hand, we can redefine Get Expanded methods to handle all cases at once.

In few real time scenarios we encounter multi level expansions and also Single parent and Multiple child relationship. Still the same holds good with minor modifications.

Example URI for Single parent and Multiple child relationship.

URI : /sap/opu/odata/sap/ZDEEPSTRUCTURES_SRV/HeaderSet('100')?$expand=HeaderToItemNav, HeaderToItem1Nav

For multilevel expansion we have many reference blog with details.

Please feel free to suggest if any changes or mistakes encountered.

Thank you 🙂
13 Comments
Labels in this area