Create RFC Functions
This section shows how to create, register and use RFC server functions.
Register the RFC Server Function
- Initialize an RFCServer object. Provide the gateway host, the gateway service and the program ID to register on the SAP gateway. The program ID must be available as a registered destination in SAP, see Set Up an RFC Destination.
- Register an RFCServerFunction object using
RegisteredFunctions.Add
. An RFCServer object can hold more than one RFCServerFunction object. - Add Imports and Exports. They are handled the same way as when calling RFC functions as a client.
- When an ABAP program calls the function, the event IncomingCall is triggered.
When using C#, the event call-back must be defined in a separate line of code. - Start the server using
Start
.
Note
If the RFC destination is set to Unicode (SAP transaction code SM59), the property IsUnicode of the RFCServer object must be set to true.
In the following sample applications a simple RFC server function is created that allows ABAP programs to add numbers.
Two input parameters (NUMBER1 and NUMBER2) are added in a new function (Z_ADD). The result (RES) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
A table (NUMBERS) with a column (NUMB) is added to a table collection using Add()
. The result (RES) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
A structure (NUMBERS) with 2 columns (NUMB1 and NUMB2) is added to a column collection via RFCTableColumnCollection()
. The result (RES) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
Two structures (NUMBERS and EXNUMBERS) with 2 columns (NUMB1 and NUMB2) are added to column collections via RFCTableColumnCollection()
. One structure (NUMBERS) is used for import and the other (EXNUMBERS) is passed back to the calling ABAP program, see Call RFC Server Functions in ABAP.
Handle Incoming Calls
The following code shows how the IncomingCall event is handled:
The result is returned in the scalar export parameter RES.
Use the object hierarchy to iterate through the rows and add the values. The result is returned in the scalar export parameter RES.
The result is returned in the scalar export parameter RES.
The values from the IMPORT collection (numberstruc) are set in variables. Then different values are added to them and are written back to the EXPORT collection (EXnumbers).
The import parameters are passed by the calling SAP system.
The export parameters are passed back to SAP.
Call RFC Server Functions in ABAP
The following ABAP code is used to call the new function Z_ADD in the remote destination ERPTEST.
REPORT z_add_test
DATA result TYPE i.
CALL FUNCTION 'Z_ADD' DESTINATION 'ERPTEST'
EXPORTING
number1 = 26
number2 = 25
IMPORTING
res = result.
WRITE: / 'Result: ', result.
The two numbers 26 and 25 are passed, and the result 51 is passed back.
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.
The two numbers 1, 2 and 3 are passed, and the result 6 is passed back.
REPORT ZADDTEST3
DATA res TYPE i.
DATA numbs LIKE zaddstruc2.
numbs-numb1 = '1'.
numbs-numb2 = '2'.
CALL FUNCTION 'Z_ADD_3' DESTINATION 'ERPTEST'
EXPORTING
NUMBERS = numbs
IMPORTING
RES = res.
WRITE: / 'Result: ', res.
The two numbers 1 and 2 are passed, and the result 3 is passed back.
REPORT ZADDTEST4 .
.
DATA numbs LIKE zaddstruc2.
DATA exnumbers like zaddstruc2.
numbs-numb1 = '1'.
numbs-numb2 = '2'.
CALL FUNCTION 'Z_ADD_4' DESTINATION 'ERPTEST'
EXPORTING
NUMBERS = numbs
IMPORTING
EXNUMBERS = exnumbers.
WRITE: / 'First result is (added 1): ', exnumbers-numb1 .
WRITE: / 'Second result is (added 98): ', exnumbers-numb2 .
The two numbers 1 and 2 are passed, and the results 2 and 100 are passed back.