How to use SAP Table Data via RFM RFC_READ_TABLE on the fly inside UiPath

In my last experiment here to use SAP remote enabled function modules (RFM) with UiPath I showed how easy it is to call an RFM. In this experiment I want to show how to call an RFM, in this case RFC_READ_TABLE, to read any SAP table and to use this data inside an UiPath process.

Let me at first introduce the RFM RFC_READ_TABLE. To call this RFM you need only the name of the table in the parameter QUERY_TABLE. In this experiment I use also the DELIMITER parameter. The RFM delivers in the table FIELDS the structure of the SAP table and in the table DATA strings with the table data. The table of string is equivalent to a CSV file. Here an important hint: The structure DATA is from type TAB512, this means it contains a table type with a 512 byte field. The maximum length of a record may be not bigger than 512 characters. Here the interface of the RFM:

Okay, it seems not difficult to use. Here an Invoke Code procedure to call this RFM and to write the SAP table data into a variable CSVTableData.


Dim cfgParams As SAP.Middleware.Connector.RfcConfigParameters
Dim destination As SAP.Middleware.Connector.RfcDestination
Dim rfcFunction As SAP.Middleware.Connector.IRfcFunction
Dim table As SAP.Middleware.Connector.IRfcTable
Dim tableLine As SAP.Middleware.Connector.IRfcStructure
Dim strTableLine As String
Dim parts As String()
Dim part As String
Dim i As Long
Dim j As Long

cfgParams = New SAP.Middleware.Connector.RfcConfigParameters
cfgParams.Add(SAP.Middleware.Connector.RfcConfigParameters.Name, "Test" )
cfgParams.Add(SAP.Middleware.Connector.RfcConfigParameters.AppServerHost, "ABAP702")
cfgParams.Add(SAP.Middleware.Connector.RfcConfigParameters.SystemNumber, "00")
cfgParams.Add(SAP.Middleware.Connector.RfcConfigParameters.Client, "001")
cfgParams.Add(SAP.Middleware.Connector.RfcConfigParameters.User, "BCUSER")
cfgParams.Add(SAP.Middleware.Connector.RfcConfigParameters.Password, "minisap")

destination = SAP.Middleware.Connector.RfcDestinationManager.GetDestination(cfgParams)

rfcFunction = destination.Repository.CreateFunction("RFC_READ_TABLE")
rfcFunction.SetValue("QUERY_TABLE", QueryTable)
rfcFunction.SetValue("DELIMITER", Separator)

table = rfcFunction.GetTable("FIELDS")
i = 0
For Each tableLine In table
  i += 1
  CSVDataTable += tableLine.GetValue("FIELDNAME").ToString.Trim
  If table.Count > i Then
    CSVDataTable += Separator
  End If
CSVDataTable += vbLf

table = rfcFunction.GetTable("DATA")
j = 0
For Each tableline In table
  j += 1
  strTableLine = tableLine.GetValue("WA").ToString
  parts = strTableLine.Split(Separator.ToCharArray)
  i = 0
  For Each part In parts
	i += 1
    CSVDataTable += part.Trim	
    If parts.Count > i Then
	  CSVDataTable += Separator
    End If
  If table.Count > j Then
    CSVDataTable += vbLf
  End If


And here my test to use it:


I checked it with the table USR01 and it works, great :slightly_smiling_face:

In the next step I want to convert the CSV data in the string to a data table. But the activity Generate Data Table detects different column types, e.g. for the client - field MANDT - or for the field STCOD - start menu.

So it seems to me more advantageous to develop my own routine to convert a CSV file as string into a data table.


  Dim Lines() As String
  Dim Line As String
  Dim Fields() As String
  Dim Field As String
  Dim Header As Boolean = True
  Dim DataColumn As System.Data.DataColumn
  Dim DataRow As System.Data.DataRow
  Dim i As Integer
  Lines = CSVDataTable.Split(vbLf.ToCharArray)
  For Each Line In Lines
    Fields = Line.Split(Separator.ToCharArray)
	i = 0
	If Header = False Then
      DataRow = DataTable.NewRow
    End If
	For Each Field In Fields
      If Header = True Then
	    DataColumn = DataTable.Columns.Add(Field, Type.GetType("System.String"))
		DataColumn.AllowDBNull = True
		DataColumn.Unique = False
        DataRow(i) = Field
	  End If
	  i += 1
	If Header = False Then
	End If
	Header = False


And it works :grinning:

On this way it is easy possible to read the content of SAP data tables and to convert it into a data table into UiPath context. Now you can use all the UiPath standard possibilities to work with the SAP data tables easily. But you have to be careful because too much data needs processing time. To minimize processing time use also the ROWCOUNT and OPTIONS parameter from the RFM.