Technical Articles
Get Expanded Entity Set /Get Expanded Entity Sap OData
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 🙂
There is an exporting parameter, et_expanded tech clause something, which needs to be filled with the navigation name that you are expanding it in that code. Else the navigation entity set will be called again..
examples:
https://blogs.sap.com/2017/09/18/generic-expanded-entity-implementation/
https://blogs.sap.com/2014/11/24/multi-level-expansion-with-getexpandedentityset/
I don't find that in your code. Did SAP change something in the new release?
Thanks,
Mahesh
Hi Mahesh,
Yes, now it will not be routed to the Entitysets. I have tried this before posting the blog.
Thank you for comment
Ohh great!! 🙂 ... Just wanted to know if it is the same if you have empty item data (Will it still skip child getEntitySet call?)
Hi Abilash KM,
I have tried to implement the same and i got the Header data but Item Results are not show in GWCLIENT. Your help is really appriciated.
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}
This is My Code
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 zitems,
wa_item type zitems.
* CONSTANTS: lc_expand_tech_clause TYPE string VALUE 'HEADERTOITEMASSO'.
case iv_entity_set_name.
when 'headerSet'.
select * from zheader into table it_header.
if sy-subrc = 0.
select * from zitems 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.
Hi Abhilash,
I got it right, Thank you.
Hi Jacob,
I am also facing similar issue .Able to find data in er_entityset of method
but not able to find data in GWCLIENT. Kindly share how you fixed the issue.
Hi Jacob and Deepthi,
I am also facing this issue. Data available in ER_ENTITYSET and not getting displayed GWCLIENT.
Kindly share the fix.
Hi,
If you are facing the issue of jacob "header data is filled, item data is empty", check the naming of the item data sub-table of your output structure. In the example above the structure of the output table is ZDEEP_S, and the sub-table is named HEADERTOITEMNAV.
It´s important, that the sub-table (item structure) is named the same as the navigation property.
cheers, Matthias
Hi Jacob,
How did you fix this?
HI Jacob, how did you fix it?
Hi Jacob/Abhilash,
I have tried the same approach as defined in the blog using my own deep structure from SE11 but not getting any data in the output odata. No data in either header or line Item data is shown after i execute the odata service in gateway. I can see the data in it_out_tab when i debugged the code but somehow data is not getting filled in output structure.
Can you please suggest what could be the issue here.
Very Nice and Informative blog. Thanks
in the line
i am getting error navigationpro not thier in primary table so what i do.