Web Service Usage in SAP

Web Service, RFC

Hello everyone!

In this article, I will show the basic working logic of a web service and its operation in an example scenario by connecting a function I created to RFC. Web services are a method used to connect SAP to a different system and provide external data flow.

RFC

Product Stock Control Application

First, I start by defining a scenario. Let's assume that a supermarket chain manages product stock using SAP systems. This company should record newly added products to stock in a table in SAP. While the quantity of products with the same material number increases, a new row should be added for each newly added product. It should also get product information from a maintenance table that the customer regularly updates.

pseudo

First, let's create the tables we will use. In our maintenance table where product information will be kept, let's add the fields for whatever information we want. At this stage, I'm only adding Product Code and Product Name.

Product List

I connected the maintenance table I created to T-Code and easily added the product information I wanted to add:

Product List Transaction Code

Now let's create the table that will hold stock information and that we will update with the web service:

Stock Check Table

After completing our tables, we need to create a function module to connect RFC. For this, let's go to SE37 T-Code and start by writing the name of the function we want to create:

SE37 Screen

We will determine the parameters of the function module we created. In my scenario, I'm adding the product code as an import parameter because I will only enter the product code and get the product information from the maintenance table. I'm also adding text parameters to the export parameters so that it returns whether the operation was successful or failed after the request is processed:

Import Parameter
Export Parameter

According to the scenario I set up, I wrote the source code of the function as follows:

FUNCTION zsg_fm_product_stok.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_PRODUCT_CODE) TYPE  ZPRODUCT_CODE_DE
*"  EXPORTING
*"     VALUE(EV_SUCCESS) TYPE  XFELD
*"     VALUE(EV_MESSAGE) TYPE  BAPI_MSG
*"----------------------------------------------------------------------

  DATA: ls_stok    TYPE zsg_stok_check,
        ls_product TYPE zsg_product_list.

  ls_stok-product_code = iv_product_code.
  ls_stok-uname        = sy-uname.
  ls_stok-datum        = sy-datum.
  ls_stok-uzeit        = sy-uzeit.

  SELECT * FROM zsg_product_list
    WHERE product_code EQ @iv_product_code
      INTO CORRESPONDING FIELDS OF @ls_product.
  ENDSELECT.

  IF sy-subrc EQ 0.
    SELECT * FROM zsg_stok_check
      WHERE product_code EQ @iv_product_code
        INTO CORRESPONDING FIELDS OF @ls_stok.
    ENDSELECT.
    IF sy-subrc EQ 0.
      ADD 1 TO ls_stok-stok.
      UPDATE zsg_stok_check FROM ls_stok.
      IF sy-subrc EQ 0.
        COMMIT WORK.
        ev_success = abap_true.
        ev_message = 'Stok güncellendi! / Stock updated!'.
      ENDIF.
    ELSE.
      ls_stok-product_name = ls_product-product_name.
      ADD 1 TO ls_stok-stok.
      INSERT zsg_stok_check FROM ls_stok.
      IF sy-subrc EQ 0.
        COMMIT WORK.
        ev_success = abap_true.
        ev_message = 'Yeni kayıt eklendi! / New record added!'.
      ENDIF.
    ENDIF.
  ELSE.
    ROLLBACK WORK.
    ev_success = abap_false.
    ev_message = 'Ürün kodu geçerli değildir! / Product code is not valid!'.
  ENDIF.

ENDFUNCTION.

To connect our created function to RFC, I change the Processing Type field to Remote-Enabled Module as shown below and Activate my function.

Attributes

To create the web service for the function, we need to make the selection Utilities->More Utilities->Create Web Service->From the Function Module. Since I'm working on a single function module, I'm creating it as a Module. If I wanted to connect more than one function module to a single web service, I would need to select Function Group.

Utulities

After our selections, the screen where we determine RFC properties will open. It's sufficient to write the necessary naming and descriptions in these fields. After completing all definitions and completing this process, we need to change some properties of the RFC we created on the opened screen. We set the Profile as Low for Username/Password usage, which is the most commonly used method as a security measure applied when calling the service, and we will be able to perform operations by entering our SAP username/password when we want to call the service.

Configuration

We need to configure Soamanager settings. Soamanager is the screen that generates the link connecting web services with SAP and where we define some properties. We can access it by typing Soamanager in the T-Code section in SAP. After typing Soamanager, we will enter the Web Service Configuration section on the screen that opens in the browser.

Soamanager

In the Object Name section, we can find it by searching for the name of the function module we created.

Soamanager

When we enter the function that appears, a blank page will open. You can create the service as shown below by saying Create Service:

Create Service

After naming, the most important thing we need to pay attention to is that we need to check the User ID/Password option in the Provider Security section. Because we had selected Low as Authentication when configuring web service settings from SAP.

Security

After all these operations, we can Republish to use the service. After republishing, we can get the generated link by clicking on the icon I show below.

Republish
Link

Now we have a Web Service URL. There are various applications where we can use this URL. I will use the SOAPUI application, which I think has an easy and convenient user interface. After starting the application, we click on the SOAP option and paste the link we copied into the Initial WSDL field:

SOAPUI

When we click OK, a screen will open asking us to enter username and password. After entering our SAP login user information here and clicking OK, our service will appear on the screen. To make the service usable, we need to enter the Authentication option and add new user authorization:

Authorization
User Authorization

After entering user information, as will be seen in the Request details, we had only specified Product Code as an import parameter. Now we can make our tests.

First, let's enter a successful record with one of the existing codes in the product list and examine the response details:

Success Response

As we can see, a record has been added to our table:

Success Table

Now let's add one more of the same product. We see the message about the stock being updated in the response detail and the stock quantity increasing in the table.

Update Response
Update Table

Finally, let's try adding a code that is not in the product list:

Error Response

We see the message about the invalid product code.

Thus, we have seen that we can perform such operations by creating Web Services to provide data flow in SAP. Different scenarios can be designed by detailing the function content. I hope this has been a helpful document for the first step.


References: Burak Kocaaslan Udemy Course

← Back to TECHNOBLOG