Implementing Expand Entity/Entity Set
Hi Folks,
Finally got some time to write a Blog on something which was a common question from a lot of audience here, so doing my bit of help here .. hope this helps a lot of beginners and not for experts 😉
SAP Documentation for Expand Understanding
https://help.sap.com/saphelp_gateway20sp08/helpdata/en/ca/c683e803494b77a2e1290b987556e2/content.htm
Some of the Blogs already posted in SCN relevant to Expand and Deep Entity .
Step by Step development for CREATE_DEEP_ENTITY operation
Requirement
Considering a basic scenario where i am using BAPI_PO_GETDETAIL which has multiple output tables and input is PO number
Now we shall start with SAP Gateway
Create Project in SEGW
Create three entity types and Entity Sets
Entity Type-1- Header
Entity Type-2- Item
Entity Type-3- Schedule
Entity Set-1- HeaderSet
Entity Set-2- ItemSet
Entity Set-3- ScheduleSet
Create Association
Association-1 – AssItem (Without key fields mapping)
Association-2 – AssSchedule (Without key fields mapping)
Create Navigation
Navigation-1 – NavItem
Navigation-2 – NavSchedule
Let’s generate runtime artifacts. Click on generate runtime objects button. It will display
popup . Keep the default class names as-is and click on enter button.
Once generation is successful, you will get 4 classes. 2 for Data provider and 2 for Model provider.
we have to Redefine the method/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET
Code Snippet
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.
*————————————————————————-*
* Deep Structure
*————————————————————————-*
DATA: BEGIN OF ls_order_items.
INCLUDE TYPE zcl_zproj_982_mpc=>ts_header.
DATA: navitem TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_item WITH DEFAULT KEY.
DATA: navschedule TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_schedule WITH DEFAULT KEY,
END OF ls_order_items,
ls_item1 TYPE zcl_zproj_982_mpc=>ts_item,
ls_schedle1 TYPE zcl_zproj_982_mpc=>ts_schedule.
*————————————————————————-*
* Data Declarations
*————————————————————————-*
DATA : ls_item TYPE zcl_zproj_982_mpc_ext=>ts_item,
lt_item TYPE TABLE OF zcl_zproj_982_mpc_ext=>ts_item,
ls_sch TYPE zcl_zproj_982_mpc_ext=>ts_schedule,
lt_sch TYPE TABLE OF zcl_zproj_982_mpc_ext=>ts_schedule,
ls_header TYPE zcl_zproj_982_mpc_ext=>ty_header,
lthead TYPE STANDARD TABLE OF bapiekkol,
lshead TYPE bapiekkol,
lsitem TYPE bapiekpo,
ltitem TYPE STANDARD TABLE OF bapiekpo,
lv_filter_str TYPE string,
lt_filter_select_options TYPE /iwbep/t_mgw_select_option,
ls_filter TYPE /iwbep/s_mgw_select_option,
ls_filter_range TYPE /iwbep/s_cod_select_option,
ls_expanded_clause1 LIKE LINE OF et_expanded_tech_clauses,
ls_expanded_clause2 LIKE LINE OF et_expanded_tech_clauses,
lv_ebeln TYPE ebeln,
lt_order_items LIKE TABLE OF ls_order_items,
ltsch TYPE STANDARD TABLE OF bapieket,
lssch TYPE bapieket.
*————————————————————————-*
* Entity Set – HeaderSet
*————————————————————————-*
CASE iv_entity_set_name.
WHEN ‘HeaderSet’.
LOOP AT it_filter_select_options INTO ls_filter.
LOOP AT ls_filter–select_options INTO ls_filter_range.
TRANSLATE ls_filter–property TO UPPER CASE.
CASE ls_filter–property.
WHEN ‘PONUMBER’.
lv_ebeln = ls_filter_range–low.
WHEN OTHERS.
” Log message in the application log
me->/iwbep/if_sb_dpc_comm_services~log_message(
EXPORTING
iv_msg_type = ‘E’
iv_msg_id = ‘/IWBEP/MC_SB_DPC_ADM’
iv_msg_number = 020
iv_msg_v1 = ls_filter–property ).
” Raise Exception
RAISE EXCEPTION TYPE /iwbep/cx_mgw_tech_exception
EXPORTING
textid = /iwbep/cx_mgw_tech_exception=>internal_error.
ENDCASE.
ENDLOOP.
ENDLOOP.
*————————————————————————-*
* Call Method-BAPI_PO_GETDETAIL
*————————————————————————-*
CALL FUNCTION ‘BAPI_PO_GETDETAIL’
EXPORTING
purchaseorder = lv_ebeln
items = ‘X’
schedules = ‘X’
IMPORTING
po_header = lshead
* PO_ADDRESS =
TABLES
* PO_HEADER_TEXTS =
po_items = ltitem
po_item_schedules = ltsch.
*————————————————————————-*
* Fill Header Values to Deep Structure
*————————————————————————-*
ls_order_items–ponumber = lshead–po_number.
ls_order_items–ccode = lshead–co_code.
ls_order_items–doctype = lshead–doc_type.
*————————————————————————-*
* Fill Item values to Deep Structure
*————————————————————————-*
LOOP AT ltitem INTO lsitem.
CLEAR ls_item1.
ls_item1–ponumber = lsitem–po_number.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_OUTPUT’
EXPORTING
input = ls_item1–ponumber
IMPORTING
output = ls_item1–ponumber.
ls_item1–poitem = lsitem–po_item.
ls_item1–material = lsitem–material.
APPEND ls_item1 TO ls_order_items–navitem.
ENDLOOP.
*————————————————————————-*
* Fill Schedule values to Deep Strcture
*————————————————————————-*
LOOP AT ltsch INTO lssch.
CLEAR ls_item1.
* ls_item1-ponumber = lsitem-po_number.
ls_schedle1–poitem = lssch–po_item.
ls_schedle1–serial = lssch–serial_no.
APPEND ls_schedle1 TO ls_order_items–navschedule.
ENDLOOP.
*————————————————————————-*
* Assign the Navigation Proprties name to Expanded Tech clauses
*————————————————————————-*
ls_expanded_clause1 = ‘NAVITEM’.
ls_expanded_clause2 = ‘NAVSCHEDULE’.
APPEND ls_expanded_clause1 TO et_expanded_tech_clauses.
APPEND ls_expanded_clause2 TO et_expanded_tech_clauses.
*————————————————————————-*
* Append Deep Strcture Values to Final Internal Table
*————————————————————————-*
APPEND ls_order_items TO lt_order_items.
*————————————————————————-*
* Send back Response to Consumer
*————————————————————————-*
copy_data_to_ref(
EXPORTING
is_data = lt_order_items
CHANGING
cr_data = er_entityset ).
WHEN OTHERS.
ENDCASE.
ENDMETHOD.
Coding Part Done….Lets move to Testing 🙂
Test Case 1:
URI : /sap/opu/odata/sap/ZPROJ_982_SRV/HeaderSet?$filter=PoNumber eq ‘4500000163’&$expand=NavItem
Test Case 2:
URI : /sap/opu/odata/sap/ZPROJ_982_SRV/HeaderSet?$filter=PoNumber eq ‘4500000163’&$expand=NavItem,NavSchedule
For Expand Entity :-
From the modelling point of view there wont be any changes
but in DPC we need to Redefine the method /iwbep/if_mgw_appl_srv_runtime~get_expanded_entity.
Also there would be a small change in Code , Like Below
*————————————————————————-*
* Deep Structure
*————————————————————————-*
DATA: BEGIN OF ls_order_items.
INCLUDE TYPE zcl_zproj_982_mpc=>ts_header.
DATA: navitem TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_item WITH DEFAULT KEY.
DATA: navschedule TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_schedule WITH DEFAULT KEY,
END OF ls_order_items,
ls_item1 TYPE zcl_zproj_982_mpc=>ts_item,
ls_schedle1 TYPE zcl_zproj_982_mpc=>ts_schedule.
copy_data_to_ref(
EXPORTING
is_data = ls_order_items
CHANGING
cr_data = er_entity ).
Hope this Blog now helps you to implement Expand Entity/Entityset
Waiting for your feed back and suggestions and more questions 🙂
Thanks
Sri
Well explanation with simple example
Thanks Syam
Nice one srikanth, very helpful..
Thanks vijay
Great
Thanks Ashwin
Nice one.....
.Very helpful to beginners.
thanks Imran
good one ..thanks Sri
Thanks Prashanth
Nice article for beginners like us !! Thank you !!
Hi Sri,
why are you using the method GET_EXPANDED_ENTITYSET when you are calling the BAPI 'BAPI_PO_GETDETAIL' with a single PO number as a key ?
I would have expected GET_EXPANDED_ENTITYSET to be used when a method BAPI_PO_GETLIST would be called instead?
Using GET_EXPANDED_ENTITYSET without using a proper filtering can have some unwanted side effects and in a typical UI you would not expand all subnodes of a list but only those of a selected item. For example the items of a purchase order including the schedule or the purchase orders of one business partner.
Best Regards,
Andre
Hi Andre,
Thanks for your comment and Feedback, Agree on the points mentioned by you
i just took this as one of the examples and not for BAPI_PO_DEATAIL
thanks
Sri
Don't get me wrong. I really like your post.
Best Regards,
Andre
Hi Srikanth, I have question. what is need of Association and Navigation? Could you please explain it. Thanks, Prashant
Hello Prashanth,
Please go through the below for more details info.
https://help.sap.com/saphelp_nw74/helpdata/en/07/dc22512c312314e10000000a44176d/content.htm
Regards,
Ashwin
Hi,
I followed the same method for getting one header (entity type HEADER )+ multiple lines(entity type LINE belonging to the header). But it is throwing error Line entity GET_ENTITYSET is not implemented in Gateway client .
In debugger I found that its GET_EXPANDED_ENTITYSET is called with LINE entity type also.I returned when entity type is LINE in the method . In that case it was showing only records of Header .
When I implemented LINE entity type GET_ENTITYSET it works fine. But the thing is I dont waht to make second call to he backend for LINE enitty type. I can get all th data in one go thats why i used GET_EXPANDED_ENTITYSET
Regards
Arshad
Nice article...very helpful to beginners !!! Thank you !!!
I have implemented the expanded_entity method as per the above code but i haven't received the dependent (child) details.
Could you please suggest me what might be the problem?
Thanks
Hello Dikshith,
Child details is not seen in the final response is it ? this is wat u mean i am unable to receive details of the child ?
Please check the deep structure which u created in the DPC/DPC_EXT class.
Names used while creating this should be as same as the navigation property which u have created while modelling ur GW service.
Regards,
Ashwin
Thank you Srikanth Gajula,
it got worked
Hi Srikanth,
Awesome guide!
Very very very helpful!
Thank you!
Very nice Sri, thanks a lot for sharing.
Warm Regards
Hemendra
Srikanth,
Keep up dude..
Regards,
Pavan Golesar
abaper.weebly.com
Thanks Sri. I have a question. Can I add filter to the expand entity? For example/sap/opu/odata/sap/ZPROJ_982_SRV/HeaderSet?$filter=PoNumber eq '4500000163'&$expand=NavItem?$filter='XYZ' eq '12345'.
Very helpful, thanks for sharing!
Hi Sri,
I followed the same stpes, but for me GET_EXPANDED_ENTITYSETis working fine but GET_EXPANDED_ENTITY not returning any values.
In the method GET_EXPANDED_ENTITY, the value getting passed to the structure er_entity.But in the method READ_EXPANDED_ENTITY (/IWBEP/CL_MGW_ABS_DATA) the value not passing to the structure cr_entity.
IF lr_entity NE cr_entity.
*-----it isn't the same ref - required to copy the data to the deep structure ref - deep copy if required
/iwbep/cl_mgw_data_helper=>move_corresponding(
EXPORTING
ir_source_data = lr_entity
IMPORTING
er_target_data = cr_entity
).
ENDIF. ( Not working).
Navigation property & Deep structure name also same.
Please help me on which case this issue may occur.
Thanks
Hi Sathish,
Even i faced the same issue, Pls check if the component names of cr_entity and lr_entity are same while debugging... this can happen when you define declare a deep structure within your get_expanded_entity.
in my case there was a mismatch in the deep structure name which i had defined within the get_expanded_entity method.
if you see a mismatch, correct the component name accordingly in the data declaration of get_expanded_entity method.
Just make sure you keep the following names same : e.g., ORDERTOITEM
1. Item table name within the deep structure
2. NAV Property name for Header Key Field.
3. ABAP Fieldlist name, this you will find when you open the association in the Header(SEGW)
Hi.
I'm getting the same error. I'm debugging the method and when i reach the READ_EXPANDED_ENTITY returning cr_entity to lr_entity the structure is the same in both sides, but with no values...
Does someone has the same issue?
I followed your steps and it seems everything is ok.
I found some OSS notes related to $expand:
2005197 - $expand request does not return all inlines
1995145 - $expand request URI returns empty inlines
2120202 - Problem with $expand and $select
I 'll ask to implement them and see if the problem persists or not.
Regards.
Mauricio.
Nice Blog,seems quite helpful ..
Hello,
I am not getting the data in the header structure. kindly help.
Hi,
I have similar requirement, I have implemented required code...on debugging I'm able to see all values getting filled in deep structure.
However, in result, I get only the header properties and all inline feed(items) are not getting filled.
Can someone help?
Hi Srikanth,
I implemented the GET_EXPANDED_ENTITYSET also getting proper values, but it is triggering 3 times. Can you please tell me how can I avoid this issue.
Thanks & Regards,
Ayyappan.V
I followed the same process it is working for get expanded entity set but,when i am passing key (get expanded entity) it is not showing any vales .
output:
},
"AUFPL"Â :Â "",
"AUFNR"Â :Â "",
"ordertooperation"Â :Â {
"results"Â :Â [
]
}
Hi Sri,
First of all thank you for the helpful blog.
I was going trying to replicate this scenario in my sandbox system but struck at below point.
How did you create the Association between "Header" & "Schedule" entities, these do not have any common key fields between them.
I wonder how the association is working here.
Also can we association till 2-3 levels. I mean from "Header"->"Item"->"Schedule"?
Thanks,
Praveen Bitling
Great blog!
Just one quick question for you experts.
I have seen that associations are created without key association. Any reason for that?
Is it the case that when just expand is required, and not normal navigation header(1212)/item, the key mapping must not be added?
Regards,
Many thanks.
C.