Function Import in SAP OData Service

3
29558

Hello everyone, in this tutorial we will learn how to implement Function Import in SAP OData Service.

What is Function Import?

Function Imports are the actions like release of a sales order/purchase order executed in the back-end system.

Function Imports can be used when ever the given requirement does not fit into the CRUD-Q operations.

They are defined at service level and you can have N number of function imports functions.

Business Scenario

We have a table in which it contains customer information, two or more customers information have to be approved at once. In this scenario to approve each customer information we will use Function Import.Table data

Create Function Import in SAP 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.

Gateway Service Builder3. Specify the name of the function import and click on green tick mark button.

Create Function Import4.  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, specify the name of the complex entity type in this field.
  • If you choose Entity Type option in the above step, specify the name of entity type.
  • If you choose No return, this field will be empty.

Return Cardinality:

‘1’ or ‘n’ based on the Return Type Kind defined above. if ‘n’ is selected provide the entity set name

HTTP Method Type:

Choose the HTTP method type to invoke the Function Import.

  • GET , when you want to get the data.
  • POST, when you want to post the data to back-end.

Action for Entity Type:

This field is used to identity to which entity type in the service, the function import is applied as an ACTION. Specify the entity type name available in the service.

Label:

Displays object label.

In our business requirement we have set the following options

Function Import Options5. 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.

Import Parameters6. 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 run-time 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.

Service metadataImplement Function Import

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

        READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'Approved_Flag'.
        IF sy-subrc = 0.
          lv_flag = ls_parameter-value.
        ENDIF.

        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'.
          ELSE.
            ls_entity-type   = 'E'.
            ls_entity-message = 'Error'.
          ENDIF.


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

        ENDIF.
      ENDIF.
    ENDIF.

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

/sap/opu/odata/sap/ZTEST_GW_SRV_SRV/demoFuncImport?Approved_Flag=’X’&CustId=’0000000001′

Change the HTTP method to POST and hit execute button. You should see the success message in output like below.

Function Import OutputGo back to table and check whether the approval flag has been set to ‘X’.

OutputPlease stay tuned to us for more SAP OData Tutorials. Please feel free to comment and let us know your feedback. Thank you. 🙂

WordPress database error: [Table 'saplearn_wp1.wp_nsex_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_nsex_comments.comment_ID FROM wp_nsex_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1151 AND comment_parent = 0 ORDER BY wp_nsex_comments.comment_date_gmt ASC, wp_nsex_comments.comment_ID ASC

Comments are closed.