Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

ABAP Web Services and Remote Function Calls (RFCs)

ABAP Web Services and Remote Function Calls (RFCs)

In ABAP (Advanced Business Application Programming), you can integrate with external systems and services using Web Services and Remote Function Calls (RFCs). Web Services allow for communication over HTTP, while RFCs enable communication between SAP systems or SAP and non-SAP systems. Below, you'll find examples of both:

1. Web Services in ABAP:

Creating a Web Service in ABAP and consuming it:

Step 1: Create a Web Service in ABAP:

Let's create a simple Web Service that converts temperature from Fahrenheit to Celsius.

FUNCTION Z_FAHRENHEIT_TO_CELSIUS
  EXPORTING
    VALUE(INPUT_TEMP) TYPE f
  RETURNING
    VALUE(OUTPUT_TEMP) TYPE f.

  OUTPUT_TEMP = ( INPUT_TEMP - 32 ) / 1.8.

ENDFUNCTION.
   

 

Step 2: Create a Web Service:

  • Go to transaction SE80.
  • Select "Enterprise Services" and create a new Service Definition (or select an existing one).
  • Create an operation for your service definition and bind it to the function module created in step 1.
  • Generate and activate your service.

Step 3: Consume the Web Service:

You can consume this Web Service from an external system (e.g., a Java application) using SOAP or REST protocols.

2. Remote Function Calls (RFCs) in ABAP:

Creating an RFC-enabled function module and calling it remotely:

Step 1: Create an RFC-enabled Function Module:

Create a function module that you want to call remotely. For example, let's create an RFC-enabled function module to retrieve employee data.

FUNCTION Z_GET_EMPLOYEE_DETAILS
  EXPORTING
    VALUE(EmployeeID) TYPE i
  TABLES
    EmployeeData STRUCTURE ZEMPLOYEE.
       

 

Step 2: Register the Function Module for Remote Calls:

  • Go to transaction SE37.
  • Find the function module and set it to "Remote-Enabled Module" under the "Attributes" tab.
  • Save and activate the function module.

Step 3: Create an RFC Destination:

  • Go to transaction SM59.
  • Create an RFC destination to the target system where you want to call the RFC.
  • Configure the connection settings.

Step 4: Call the RFC Remotely:

You can call the RFC from an external system (e.g., a Java application) using various protocols, such as SOAP, JSON-RPC, or direct RFC calls.

Here's an example using Python and the pyrfc library to call the RFC from a non-SAP system:

from pyrfc import Connection

# Define RFC connection parameters
conn_params = {
    "ashost": "SAP_SERVER_HOST",
    "sysnr": "00",
    "client": "100",
    "user": "YOUR_USERNAME",
    "passwd": "YOUR_PASSWORD",
}

# Create an RFC connection
conn = Connection(**conn_params)

# Call the RFC function
result = conn.call("Z_GET_EMPLOYEE_DETAILS", EmployeeID=123)

# Process the result
print("Employee Name:", result["EmployeeData"]["EmployeeName"])
print("Employee Age:", result["EmployeeData"]["EmployeeAge"])
     

 

This Python script establishes an RFC connection to the SAP system and calls the Z_GET_EMPLOYEE_DETAILS function module remotely, passing an employee ID. The function module returns employee details, which are then printed in the Python application.