In this blog, I will cover the basics of using deep structures using get Expanded Entityset and Entity in OData service.
In some cases, we are required to fetch parent child relationship data in a single call or we may have to save header item details in database.
OData provides an option to perform this operations using deep structures.
Excercise: Example on Fetch/Get Scenario
Header to Item(1 to 1)
We will use, ID as the relationship between these two tables
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
{
"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
ConversionConversion EmoticonEmoticon