Skip to Content

Hi All,

        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

e1.JPG

e2.JPG

    ii)Now create a new OData Development project and select OData Model

e3.JPG

    iii)Select Blank OData model and the folder should be pointing towards the black SAP UI5 project you have created before.

e4.JPG

    iv)In the OData editor, you can create Entity Types, Navigation, Associations etc via the GUI editor.

e5.JPG

    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).

/wp-content/uploads/2014/05/e8_460920.jpge6.JPG

    vi)You can directly edit the properties of each field in the properties tab as shown belowe7.JPG

    vii)Add all the necessary fields for the Entity Type.

e18.JPG

    viii)Now right click on the project and click on export, select OData Model under OData Development.

e9.JPG

    ix)Select OData V2 SAP Specific from the drop down list

e11.JPG


Step 2-Function Import creation in OData

    i)Create a new project and Import OData Model into Gateway Service Builder

1.JPG

2.JPG

    ii)Browse to the exported OData Model file

/wp-content/uploads/2014/05/u1_460930.jpg

/wp-content/uploads/2014/05/u2_460931.jpg

    iii)Choose the fields required for your Entity Type

/wp-content/uploads/2014/05/u3_460932.png

    iv)Entity type is being created

/wp-content/uploads/2014/05/u4_460933.jpg

/wp-content/uploads/2014/05/u5_460934.jpg

    v)Create the corresponding Entity Set

/wp-content/uploads/2014/05/u6_460935.jpg

    vi)Here specify the features that you want for your entity sets by checking the checkboxes

/wp-content/uploads/2014/05/u7_460936.jpg

4.JPG

    vii)Now right click on the Function Import and click create

/wp-content/uploads/2014/05/6_460938.jpg

    viii)Give the name for your Function Import

7.JPG

/wp-content/uploads/2014/05/8_460952.jpg

    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.

9.JPG

    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.

14.JPG

    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 .

/wp-content/uploads/2014/05/10_460954.jpg

    xii)Since ours is a custom method, we have to Go to Method /IWBEP/IF_MGW_APPL_SRV_RUNTIME

/wp-content/uploads/2014/05/u8_460979.jpg

    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)

Code Sample


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_parametervalue.
READ TABLE it_parameter INTO ls_parameter WITH KEY name = ‘conn_id’.
lv_conn_id
= ls_parametervalue.


SELECT  * FROM sbook INTO CORRESPONDING FIELDS OF
TABLE lt_entityset 
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 ).

ENDIF.

15.JPG

16.JPG

     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)

17.JPG

     xv) /sap/opu/odata/sap/ZFM_IMPORT_SRV/Flt_Bk_Hst?carr_id=’AA’&conn_id=’19’

          Now use the above URL format to access the function import and the input parameters which is separated by ‘&’.

19.JPG

     xvii)You can see the output of the OData with corresponding Booking Details against the Keys.

20.JPG

     xviii)Output in browser

21.JPG

23.JPG

24.JPG

Regards,

Bince Mathew

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

    1. Carlos Roggan

      Hi Filippo,

      what you can do  is to register your service, then invoke the $metadata document in a browser and download the xml source to a file.

      This file can then be imported in Eclipse, while creating a new OData Model, you have the choice it create from “OData Metadata File”.

      Or you can use File -> Import -> OData Model

      Alternatively, you can do the same directly from the “OData Service URL”

      Cheers,

      Carlos

      (0) 
  1. Sean Machado

    Hi

    At Step xiv) you failed to mention that before you test the OData Service, (Transaction Code: /IWFND/GW_CLIENT) you need to Activate the Service under the Service Maintenance folder, by double-clicking something like GATEWAYSERVER. Or at least that is what I had to do.

    In fact, I had to go to SPRO to create the GATEWAYSERVER in SAP Gateway Settings as per another document (http://go.sap.com/documents/2012/11/44779e8c-5b7c-0010-82c7-eda71af511fa.html)

     

    (0) 

Leave a Reply