Hello everyone, in this tutorial we will learn about Function Import in SAP Gateway OData Service and how to implement Function Import.
1.What is Function Import ?
Function Imports are the actions like release of a sale-ale settingss order/purchase order executed in the backend system.We use Function Imports when ever the requirement the does not fit into the CRUD-Q operations.They are defined at service level and you can have N number of function imports functions.
We have a table in which it contains customer information. Each every customer information have to be approved to make use of it. In this scenario to approve each customer information we will use Function Import.
3.1.Create Function Import in SAP Netweaver OData Service
1. Go to Gateway Service Builder SEGW and choose the project in which you want to create Function Import.
2. Expand the project, right click on Data Model and choose Create and the Function Import.
3. Specify the name of the function import and click on green tick mark button.
4. In the next screen you will find different options for the Function Imports lets have a look at each of them.
Return Type Kind : If you want the Function Import to return data you can set this field. We have 3 possible options available
Complex Type – if the return data is of complex entity type, select this option
Entity Type – if the return data is of entity type, select this option and
No return – if you do not want to return anything, select this option.
Return Type : This field depends on the option you have choose above. You need to specify the name of the type in this field. Use the input help to choose from existing types or create a new type.
If you choose Complex Type in the above step, you need to specify the name of the complex entity type in this field and specify the name of entity type if you choose Entity Type option in the above step.
if you choose No return, this field will be empty.
Return Cardinality :[adsenseyu1]
HTTP Method Type : Choose the HTTP method type to invoke the Function Import. Choose GET , when you want to get the data from the backend using Function Import. Choose POST, when you want to push data to backend using Function Import.
Action for Entity Type : This field is used to identity to which Entity Type in the service, the Function Import is applies as ACTION. Specify the entity type name available in the service.
In our business requirement we have set the following options
5. In this step we need to create Import Parameters to the Function Import we have created in the above step.In our business case, we need to two importing parameters Customer Id and Approval Flag. Create them and Save the parameters.
6. Till now we have defined the Function Import and our next step is to implement it before going further we want to make sure that every thing is OK till now.Save the project and click on Generate to generate runtime objects. We should see the Function Import in the meta data of the service.
Go to Netweaver Gateway Client /IWFND/GW_CLIENT and specify the service name with meta data URI Option and Execute.
7. Now we need to implement the Function Import and this is done in the DPC_EXT class and method is /IWBEP/IF_MGW_APPL_SRV_RUNTIME~EXECUTE_ACTION. Go the DPC_EXT class and redefine the method. Put the below code in the method.
DATA: ls_parameter TYPE /iwbep/s_mgw_name_value_pair,
lv_custid TYPE kunnr,
lv_flag TYPE char1,
lt_custinfo TYPE TABLE OF ztest_gw_srv,
ls_custinfo TYPE ztest_gw_srv,
ls_entity TYPE zcl_ztest_gw_srv_mpc=>ts_msg_return.
IF iv_action_name = 'demoFuncImport'. " Check what action is being requested
IF it_parameter IS NOT INITIAL.
* Read Function import parameter value
READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'CustId'.
IF sy-subrc = 0.
lv_custid = ls_parameter-value.
READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'Approved_Flag'.
IF sy-subrc = 0.
lv_flag = ls_parameter-value.
IF lv_custid IS NOT INITIAL.
UPDATE ztest_gw_srv SET approved = lv_flag WHERE cust_id = lv_custid.
IF sy-subrc = 0.
ls_entity-type = 'S'.
ls_entity-message = 'Customer info successfully approved'.
ls_entity-type = 'E'.
ls_entity-message = 'Error'.
* Call method copy_data_to_ref and export entity set data
copy_data_to_ref( EXPORTING is_data = ls_entity
CHANGING cr_data = er_data ).
The above code will update the status of APPROVED field which exists in the table.If the update is successfully it will return the success message or will return the error message.
8. So we are ready with Function Import implementation, it’s time to test the service.Go to Netweaver Gateway Client /IWFND/GW_CLIENT and specify the service name with Function Import name and its importing parameters. The Url will look like below
Change the HTTP method to POST and hit execute button.You should see the success message in output like below.
Go back to table and check whether the approval flag has been set to ‘X’.
Please stay tuned to us for more SAP Netweaver Gateway Tutorials.Please feel free to comment and let us know your feedback. You feedback will keep us alive.