Netweaver Gateway Development

how to implement nested OData($expand query option) in SAP?



                    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.
                      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_entityset method.

Pre-requisites

  1. GET_ENTITYSET of flightset implemented already.(GET_ENTITYSET implementation)
  2. GET_ENTITY of flightset implemented already.(Refer OData Service Implementation for GET_ENTITY)
  3. FlightScheduleSet created by importing ddic structure SPFLI table(Please refer  my previous post step-by-step-gateway-odata-service)
Syntax



Steps

  1. 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.
  2. 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.
  3. You will land on step 2 of association wizard.Select dependent property and click next.Dependent property will be the key relating two entities.
  4. In step 3 select Principal entityset and dependant entityset and click finish.
  5. 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.
  6. Now open our ****_DPC_EXT class and go to edit mode.

  7. From inherited methods node find /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET and right click and press redefine.
  8. Its time for some coding.Paste below code inside GET_EXPANDED_ENTITYSET method.


  9. 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?$expand=ToFlightSchedules.Press execute button.ToFlightSchedules is the navigation property.
see how to bind nested odata($expand query) in SAPUI5 Application
If you enjoyed this post, I’d be grateful if you’d help it spread by sharing.Thank you! See you soon

4 comments :

  1. Hello Rajeesh,

    I am wondering will this logic suits if more than two entities should be expanded? For now I'm getting the error message: "RFC Error: The XML document that was read had an invalid format.", when testing with three entity sets.

    Thanks!

    ReplyDelete
    Replies
    1. Hi,
      It will work.I have tested it with a third entity called flightbookings.My code changes are below.
      //in structure section
      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,
      toflightbooking type zcl_ztest_odata_01_mpc_ext=>tt_flightbooking,
      end of str_exp.
      //in constant section
      constants: lc_expand_tech_clause type string value 'TOFLIGHTSCHEDULES,TOFLIGHTBOOKING'.
      //Also inside the main loop i have added one loop to get flight bookings

      Hope this helps

      Delete
  2. I have implemented my Service in similar way but the control is not going to get_expanded_entity or get_expended_entityset.
    Any pointers?

    ReplyDelete
    Replies
    1. Hi Indu,
      Have you made association between two entities?
      also check the structure
      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,
      //see data: above...
      end of str_exp.

      also check if you have added

      insert lc_expand_tech_clause into table et_expanded_tech_clauses.
      in last line..

      sorry for late reply..:)

      Delete

Powered by Blogger.