Python call RFC to read table in SAP.

 · 3 mins read

Table of Contents

Introduction

Code example

Work around limitation reader table


Introduction

Nowadays, we see more and more tools and developments with Machine Learning in Python and sometimes it’s important to return the results of the ML models into SAP. So, in this article we will focus on a simple use of the python library “pyrfc” to be able to communicate with SAP.

first of all, we need the following requerements:

  • SAP NetWeawer RFC SDK link
  • PyRFC link
  • Some SAP system

After, we have to understand the difference between RFC and BAPI.

What is RFC?

RFC or “Remote Function Call”. It’s a method that allows a business application to exchange information with different systems. In addition, it’s an SAP protocol for establishing communication between systems. It’s possible to call a function in a different program in the same machine or in another machine using RFC, but this mechanism is usually used to call functions running on a different machine.

What is BAPI?

BAPI or “Business Application Programming Interface”. It’s an interface of standardized programming for business object models in SAP products. In addition, it encapsulates the internal layers of SAP’s business object model to confirm that all logic, authorization and validation operations executed correctly when business objects are accessed or modified.

Usage

  • RFC allows calling function modules that reside on different machines.
  • BAPI, on the other hand, allows access to SAP functions via formal, stable, dialog-free interfaces. This is another difference between RFC and BAPI in SAP.

After this little summary, in this article we will focus on the RFC option to call the function “RFC_READ_TABLE”.

Let’s get to work :)

Code example

SAP Client connection

Before to call any function in SAP, it’s necessary to make connection with system. Note: If your SAP is in Virtual Private Network (VPN), please remenber to turn it on when using these functions.

Import the library pyrfc and create an instance of connection with all the parameters used generally in your SAP logon. In this part, I propose to pass all configuration as a dictionary and keep it in env variables. Note: We include also the pandas library.

from pyrfc import Connection
import pandas as pd

con = Connection(
    user=config["user"],
    passwd=config["password"],
    mshost=config["mshost"],
    msserv=config["msserv"],
    sysid=config["sysid"],
    group=config["group"],
    saprouter=config["saprouter"],
    client=config["client"],
)

Read table in SAP with call RFC_READ_TABLE

In SAP standard library there is function module called RFC_READ_TABLE. We can use it to make a select data from SAP like SE16.

So, we will use the method created in the Connection class instance. The first parameter is the function name “RFC_READ_TABLE” and the rest of parameters are the same used in the function module in SAP. It’s possible to check in SE37 T-code in SAP.

In this example we will call the table T001W with the fields “WERKS” & “LAND1” and the information will be separated by “;”.

response = con.call(
    "RFC_READ_TABLE",
    QUERY_TABLE="T001W",
    DELIMITER=";",
    FIELDS=["WERKS", "LAND1"],
 #   OPTIONS="",
 #   ROWCOUNT="",
 #   ROWSKIPS="",
)

After with the return call function values, we can split and include all the values in the data frame “result”.

result = pd.DataFrame(
    [[x.strip() for x in response['DATA'][n]['WA'].split(";")] for n in range(len(response['DATA']))],
     columns=[x['FIELDNAME'] for x in response['FIELDS']]
     )

Check the 10 first values.

result.head(10)

Limitation of function RFC_READ_TABLE

Unfortunately, RFC_READ_TABLE has some limitations in the length of the retrieved columns must not exceed 512 characters. To solve this point I can propose to create a new Z_RFC_READ_TABLE

Procedure

  1. Use transaction SE37 to copy the module RFC_READ_TABLEt o a custom name, for example Z_RFC_READ_TABLE
  2. Use transaction SE11 to copy the structure TAB512 to a custom name, for example ZTAB4096
  3. Modify the WA member of ZTAB4096 to component type CHAR with length 4096
  4. Save and activate the new structure.
  5. Open the newly created Z_RFC_READ_TABLE and change the type of the TABLES parameter DATA to ZTAB4096

Now in your code call.

response = con.call(
    "Z_RFC_READ_TABLE",
    QUERY_TABLE="T001W",
    DELIMITER=";",
    FIELDS=["WERKS", "LAND1"],
 #   OPTIONS="",
 #   ROWCOUNT="",
 #   ROWSKIPS="",
)

Thank you for the initial photo.

Photo by Matteo Paganelli on Unsplash