Netweaver Gateway Development

Function Import in SAP Gateway OData service

               Odata includes standard CRUD operations that map to http methods GET,POST,PUT/MERGE,DELETE. In addition to these OData support another operation which is called function imports which can be invoked by http GET or POST methods.
           Function import creations are easy.Dont go for a function import if the standard CRUD methods can fulfill your requirement.Some examples when we need a function import are given below
  1. Confirm Workitem
  2. Check Flight availability
           Function imports are actions that are to be performed on entities.These are defined at service level and we can create N number of function imports.
           In this tutorial we will create a function import for knowing a particulat flight fare.
  1. OData service created already.
  1. Go to tcode SEGW.Open our project ZTEST_ODATA we created already.Click on the project  and go to edit mode.Expand project node and right click on Data Model node and select Create->Function Import.This will open up Function Import creation window. 
  2. Give the function import name and click okay(tick) button.I have given CheckFlightFare as function import name.
  3. Now a new node name Function Imports will be created under Data Model node of our project.See screenshots.
  4. For each function import we can define return kind.Three return kinds supported are Complex type,Entity type and No return.In this function import we need to return some data so that we need to create an entity type and entityset for this.So again right click on Data Model and select Create->Entity Type.
  5. Entity creation window will open.Give Entity type name.check Create Related Entity Set checkbox and give entityset name.Click okay.
  6. Open created FlightFare entity type and create two properties called Fare and Currency.Give type as Edm.string.Dont forget to set any or both of these properties as keys otherwise while generating it will throw error.Check and generate the project.
  7. Now open our Function Import CheckFlightFare by clicking on it.Select return kind as Entity type and select Return type as our newly created FlightFare entity Return Cardinality as 1.Return cardinality specify how many of return type can occur in result.Supported values are 0..1,1,0..n,1..n.Select return http method as GET.You can choose POST also according to requirements.
  8. Expand FlightFare function import node and double click on Function Import Parameters.We will add three import parameters which are flightdate,connectionid and carrierid.For flightdate type is Edm.DateTime and for others Edm.String.Check and Generate the project.
  9. Time for some coding.Open our ***DPC_EXT class and right click on EXECUTE_ACTION and click Redefine.
  10. Put below code inside the method.Here we access the function import parametes and return the Flight fare based on the query result.Activate the method.

  11.  DATA: ls_parameter TYPE / iwbep / s_mgw_name_value_pair,
         ls_entity type zcl_ztest_odata_01_mpc => ts_flightfare,
         lv_carrier TYPE zcl_ztest_odata_01_mpc => ts_flight - carrid,
         lv_connection TYPE zcl_ztest_odata_01_mpc => ts_flightschedule - connid,
         lv_fldate TYPE dats.
     IF it_parameter IS NOT INITIAL.
     DATA: BEGIN OF str_fare,
         price type sflight - price,
         currency type sflight - currency,
         END OF str_fare.
     READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'carrierid'.
     IF sy - subrc = 0.
     lv_carrier = ls_parameter - value.
     READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'connectionid'.
     IF sy - subrc = 0.
     lv_connection = ls_parameter - value.
     READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'flightdate'.
     IF sy - subrc = 0.
     lv_fldate = ls_parameter - value.
     case iv_action_name.
     when 'CheckFlightFare'.
     SELECT single price currency from sflight into str_fare where carrid = lv_carrier and connid = lv_connection and fldate = lv_fldate
     ls_entity - fare = str_fare - price.
     ls_entity - currency = str_fare - currency.
     copy_data_to_ref(EXPORTING is_data = ls_entity CHANGING cr_data = er_data).

  12. Function import implementation is finished.To check open gateway client (/IWFND/GW_CLIENT)and give Url : /sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/CheckFlightFare?carrierid='AC'&connectionid='820'&flightdate=datetime'2002-12-20T00:00:00' and press execute button.See screenshot. 
Calling Function Import from SAPUI5.
If you enjoyed this post, Please Share!!


Post a Comment

Powered by Blogger.