RFC Server with tables

A table with name NUMBERS is added to the Z_ADD function and the function is renamed to Z_ADD_2. All entries in table NUMBERS in column NUMB should be added by the .NET program.

The code below shows how to initialize the RFCServer object. Please have a close look on how to create the table NUMBERS. It is added to the Tables collection. The column NUMB is added to the empty Columns collection with Add(…).

static RFCServer s = new RFCServer();


static void Main(string[] args)
{
    s.GatewayHost = "SAPServer";
    s.GatewayService = "sapgw00";
    s.ProgramID = "ERPConnectTEST";

    s.IncomingCall += new RFCServer.OnIncomingCall(s_IncomingCall);

    RFCServerFunction f = s.RegisteredFunctions.Add("Z_ADD_2");
    f.Exports.Add("RES", RFCTYPE.INT);

    RFCTable numbertable = f.Tables.Add("NUMBERS");
    numbertable.Columns.Add("NUMB", 10, 0, RFCTYPE.NUM); 

    s.Start();

    Console.WriteLine("Press Enter to quit");
    Console.ReadLine();
}


The handling of the tables is done as always. We are using the object hierarchy to iterate through the rows and add the values. The result is returned in the scalar export parameter RES.


static void s_IncomingCall(RFCServer Sender, RFCServerFunction CalledFunction)
{
    Console.WriteLine("Incoming call!!");

    Int32 Res = 0;

    foreach (RFCStructure row in CalledFunction.Tables["NUMBERS"].Rows)
        Res += Convert.ToInt32(row["NUMB"]);

    CalledFunction.Exports["RES"].ParamValue = Res;
}

Here is the sample ABAP code to call the function in a foreign destination.


REPORT  zaddtest2                               .

DATA res TYPE i.
DATA numbs LIKE zaddstruc OCCURS 0 WITH HEADER LINE.

numbs-numb = '1'.
APPEND numbs.
numbs-numb = '2'.
APPEND numbs.
numbs-numb = '3'.
APPEND numbs.

CALL FUNCTION 'Z_ADD_2' DESTINATION 'ERPConnectTEST'
  IMPORTING
    res     = res
  TABLES
    numbers = numbs.

WRITE: / 'Result: ', res.


Creation date: 4/12/2022 9:14 AM      Updated: 1/22/2024 10:00 AM