This document covers about Function Import in OData along with an overview of Eclispe OData Modeler tool. Function import is a feature in OData that is similar to an RFC call. Function import will be having import and output parameters separate rather than using the same structure for import and export in other CRUD operations in OData
Usually function imports are used when the similar functionality cannot be achieved by CRUD operations. Function imports can be invoked as GET or POST HTTP methods.
The following are the steps involved in creating a Function Import. In this tutorial, instead of importing a DDIC structure. I will be creating the OData Model from a Eclipse OData Modeler.
Here the scenario is to create an OData to fetch the Flight Booking Details from the SBook table.
Step1– Creating the necessary OData model in eclipse
i) First create a blank UI5 project to add the OData Model into it
ii)Now create a new OData Development project and select OData Model
iii)Select Blank OData model and the folder should be pointing towards the black SAP UI5 project you have created before.
iv)In the OData editor, you can create Entity Types, Navigation, Associations etc via the GUI editor.
iv)Drag and drop the Entity Type into the editor, Right click the Entity Type and select Add Property and add new fields(Select simple).
vii)Add all the necessary fields for the Entity Type.
viii)Now right click on the project and click on export, select OData Model under OData Development.
ix)Select OData V2 SAP Specific from the drop down list
Step 2-Function Import creation in OData
i)Create a new project and Import OData Model into Gateway Service Builder
ii)Browse to the exported OData Model file
iii)Choose the fields required for your Entity Type
iv)Entity type is being created
v)Create the corresponding Entity Set
vi)Here specify the features that you want for your entity sets by checking the checkboxes
vii)Now right click on the Function Import and click create
viii)Give the name for your Function Import
ix)Select the cardinality as 1:n and then mention the HTTP Method as GET ,select your Entity Set as return entity set and the Entity Type as the same Entity Type you created before.
What is happening here is that you have to specify the return parameters for the function import, and we are using the entity set we created in this project as a return table parameter, which will be having all the booking details that is fetched from the backend Sbook table.
x)Now we need to create import parameters for your import function.(This will be the fields that you will be passing to the OData to get the table in return from the function import as the result, just like how you will be calling an RFC).
Click on new and create the two fields that we want as input parameters for the function import. In this case it will be carr_id and conn_id specify the EDM core Type and Data element.
xi)Now click on the red generate button on the upper left side of the screen and system will now create all Data Provider and Model Provider classes .
xii)Since ours is a custom method, we have to Go to Method /IWBEP/IF_MGW_APPL_SRV_RUNTIME
xiii)Redefine the method (EXECUTE_ACTION) /IWBEP/IF_MGW_APPL_SRV_RUNTIME~EXECUTE_ACTION
In this method
1) Parameter iv_action_name will contain the custom function name that you will be requesting through OData URL.
2) Parameter it_parameter will be having the key fields that you will be sending to the OData as input(ie conn_id and carr_id)
DATA: ls_parameter TYPE /iwbep/s_mgw_name_value_pair,
ls_entity TYPE zcl_zfm_import_mpc=>ts_flight_booking_history,
lt_entityset TYPE zcl_zfm_import_mpc=>tt_flight_booking_history,
lv_task_id TYPE ztask_cnt,
lv_carr_id TYPE S_CARR_ID,
lv_conn_id TYPE S_CONN_ID.
IF iv_action_name = ‘Flt_Bk_Hst’.
IF it_parameter IS NOT INITIAL.
** Read Function import parameter value
READ TABLE it_parameter INTO ls_parameter WITH KEY name = ‘carr_id’.
lv_carr_id = ls_parameter–value.
READ TABLE it_parameter INTO ls_parameter WITH KEY name = ‘conn_id’.
lv_conn_id = ls_parameter–value.
SELECT * FROM sbook INTO CORRESPONDING FIELDS OF
WHERE CARRID = lv_carr_id
AND CONNID = lv_conn_id. “Your logic to fetch your data goes here
copy_data_to_ref( EXPORTING is_data = lt_entityset “This is used to set the data back to the Odata response
CHANGING cr_data = er_data ).
xiv) Save the above code and activate it.The go to the Net weaver gateway client to test the OData.
Use /sap/opu/odata/sap/ZFM_IMPORT_SRV/$metadata (use the $metadata URI to see the custom function import name in the response body)
Now use the above URL format to access the function import and the input parameters which is separated by ‘&’.
xvii)You can see the output of the OData with corresponding Booking Details against the Keys.
xviii)Output in browser