The $expand query option is very powerful and allows you to provide multiple entities and/or entity sets in one single service call, instead of performing several calls subsequently.
In our previous tutorial we implemented GET_EXPANDED_ENTITYSET method.In this tutorial we will implement GET_EXPANDED_ENTITY method.ie $expand for single entry.
For $expand query to work an association or navigation property should be created.To know more about association refer Implement association.
In this tutorial we will create an association from our flight entity set to flight schedule entityset.Then we will implement get_expanded_entity method.
Pre-requisites
In our previous tutorial we implemented GET_EXPANDED_ENTITYSET method.In this tutorial we will implement GET_EXPANDED_ENTITY method.ie $expand for single entry.
For $expand query to work an association or navigation property should be created.To know more about association refer Implement association.
In this tutorial we will create an association from our flight entity set to flight schedule entityset.Then we will implement get_expanded_entity method.
Pre-requisites
- GET_ENTITYSET of flightset implemented already.(GET_ENTITYSET implementation)
- GET_ENTITY of flightset implemented already.(Refer OData Service Implementation for GET_ENTITY)
- FlightScheduleSet created by importing ddic structure SPFLI table(Please refer my previous post step-by-step-gateway-odata-service)
Syntax
Steps
http://<host>:<port>/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet('AC')?$expand=ToFlightSchedules
Steps
- First step is creating association.For this Open the service and click on edit button.Expand Data Model.Right click on Associations and click on create button.Association wizard will be opened.
- In the association wizard you have to give Association Name,Principal entity type name,Dependant entity type name,cardinality and Navigation Property name.Navigation Property name is used when accessing associated entity data in our OData url.(Create related navigation property check box will be checked by default).After entering values to these fields click next.
- You will land on step 2 of association wizard.Select dependent property and click next.Dependent property will be the key relating two entities.
- In step 3 select Principal entityset and dependant entityset and click finish.
- Finally our odata service should look like this.See navigation propery is added to flight entity set.Also an association and association set is created.Press generate button.
- Now open our ****_DPC_EXT class and go to edit mode.
- From inherited methods node find /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITY and right click and press redefine.
- Its time for some coding.Paste below code inside GET_EXPANDED_ENTITY method.
- Now we can test in gateway client( /IWFND/GW_CLIENT).Our service url look like this.http://<host>:<port>/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet('AC')?$expand=ToFlightSchedules.Press execute button.ToFlightSchedules is the navigation property.
data lv_entityset_name type string. data: begin of str_exp. include type zcl_ztest_odata_01_mpc_ext=>ts_flight. data: toflightschedules type zcl_ztest_odata_01_mpc_ext=>tt_flightschedule. data: end of str_exp. data: lt_expand like str_exp, ls_expand like str_exp, ls_schedule type zcl_ztest_odata_01_mpc=>ts_flightschedule. data:it_flight type table of scarr, wa_flight like line of it_flight, it_schedule type table of spfli, wa_schedule like line of it_schedule. data:ls_keytab TYPE LINE OF /IWBEP/T_MGW_NAME_VALUE_PAIR. constants: lc_expand_tech_clause type string value 'TOFLIGHTSCHEDULES'. lv_entityset_name = io_tech_request_context->get_entity_set_name( ). case lv_entityset_name. when 'FlightSet'. loop at it_key_tab INTO ls_keytab. ENDLOOP. select * from scarr into corresponding fields of table it_flight WHERE carrid = ls_keytab-value. select * from spfli into table it_schedule WHERE carrid = ls_keytab-value. if sy-subrc = 0. loop at it_flight into wa_flight. move-corresponding wa_flight to ls_expand . loop at it_schedule into wa_schedule where carrid = wa_flight-carrid. move-corresponding wa_schedule to ls_schedule. append ls_schedule to ls_expand-toflightschedules. clear ls_schedule. endloop. endloop. endif. endcase. copy_data_to_ref( exporting is_data = ls_expand changing cr_data = er_entity ). insert lc_expand_tech_clause into table et_expanded_tech_clauses.
If you enjoyed this post, Please Share!!
Hi! First of all thanks for your help with this post. I am trying to do this, but I am receving a error:
ReplyDeleteURL:
/sap/opu/odata/sap/ZTESTE_ODATA_SRV/FlightSet('AA')?$expand=ToFlightSchedules
ERROR:
In the context of Data Services an unknown internal server error occurred
Can you HELP-ME?
Check the error log '/IWFND/ERROR_LOG'.And Resolve the error
DeleteHi. Thanks for the explanation. 2 questions:
ReplyDelete1) Why do i have to redefine GET_ENTITY and GET_ENTITYSET for use GET_EXPANDED_ENTITYSET?
2) I redefined as your example but when i test it the only method called is GET_ENTITY, with or without $expand.
1.Try without redefining those methods..You will get method not implemented error while calling expanded entityset method.
Delete2.For get_entity to work first get_entityset has to work right?
Thanks for the explanation. You are absolute right! Now, i'm following your examples to implement this kind of query into a SAPUI5 app. Thanks again!
DeleteYou are welcome. Thank for feedback
ReplyDeleteWhen to use ~GET_EXPANDED_ENTITY VS GET_EXPANDED_ENTITYSET ?
ReplyDeletewhen you need to show a list of items with nested data you can use get_expanded_entityset. for example salesorders with lineitems as a list. When you need to show just a salesorder with multiple lineitems use get_expandedentity. use case is Suppose we just need to show the lineitems when clicking on a listitem in a list.
DeleteHi, i'm new to odata, i already created a deep entity to get from a fiori UI. i redefined the method Define in MPC and redefined the CREATE Deep entity. Also created an custom method just to read the data from fiori using io_data_provider. the problem is i can only get the values from the entity but for the entityset there are no data during this process. Upon debugging there are data in the entityset.
ReplyDeleteAre you using er_entityset to export data.. If Data is available as table structure it should export values
DeleteHi Rajesh,
ReplyDeleteI have a scenario where i will pass the filter values .Suppose if the record is present in the parent entity and not present in the child entity.I should not display the parent entity record in the front end . Can you help me out on that.
You can handle it in the front-end I think.. check This use of factory function in sapui5
DeleteI have implemented in same way with 2 entityset set. header displayed on the output but not item data. er_entiryset is having header and item data both
ReplyDeletedid you forget this line of code?
Delete'insert lc_expand_tech_clause into table et_expanded_tech_clauses.'
Please check
I have implemented in same way with 2 entityset set. header displayed on the output but not item data. er_entiryset is having header and item data both,also implemented 'insert lc_expand_tech_clause into table et_expanded_tech_clauses.' this line too..
ReplyDeletetry debugging using external break point. Debug Odata service
DeleteI have created in the same manner as described above.. but while testing GET_EXPANDED_ENTITYSET method triggering multiple times and the results are also coming wrong.
ReplyDeleteHere we are implementing GET_EXPANDED_ENTITY not GET_EXPANDED_ENTITYSET method.
ReplyDeleteDear Rajesh,
ReplyDeletecould you please let me know whether $expand = AB,BC/BD/DE will work or are there any limitations..
In your example ToFlightSchedules is navigation, here you are using only one navigation, suppose i have 3 navigation in same FlightSet then what will be $expand and will it work? Am facing issue with this, your comment will help..
Thanks in Advance
I think it should work. Here BC should be parent of BD and BD should be parent of DE and Also BC Should be parent of DE. Here multiple and multilevel exapand come in same query.
DeleteGood example, thanks!
ReplyDelete