Example: Calling an RFC Function with Parameters
The following example uses the Java RFC client interfaces to call an RFC function called BAPI_GL_ACC_GETLIST.
We use parts of this example in the topics that discuss the steps of
making an RFC call.RFC Function Description
The BAPI_GL_ACC_GETLIST function is the internal implementation of the GETLIST BAPI (method) for the SAP business object GeneralLedgerAccount.
We use the BAPI_GL_ACC_GETLIST function not as a demonstration of how to use the SAP BAPI technology, but rather because this RFC function offers a good combination of simple, structure and table parameters.
The BAPI_GL_ACC_GETLIST function returns the list of G/L accounts (G/L account number, short text and long text) for a specified company in a specified language.
The following table lists the parameters of the BAPI_GL_ACC_GETLIST function (note that the order of the parameters within the function is not important, since RFC uses named parameters):
Parameter Name |
Category |
Type |
Internal Name |
Mandatory? |
ACCOUNT_LIST |
Imp/Exp |
Table |
BAPI3006_1 |
N |
COMPANYCODE |
Import |
Field |
COMP_CODE |
Y |
LANGUAGE |
Import |
Field |
LANGU |
N |
LANGUAGE_ISO |
Import |
Field |
LANGU_ISO |
N |
RETURN |
Export |
Structure |
BAPIRETURN |
N |
The function returns the list of G/L accounts in the table parameter ACCOUNT_LIST, which has the following columns (note that the order of the columns in the table is important when creating and adding the fields to the table):
Field Name |
Type |
Length |
COMP_CODE |
CHAR |
4 |
GL_ACCOUNT |
CHAR |
10 |
SHORT_TEXT |
CHAR |
20 |
LONG_TEXT |
CHAR |
50 |
The function also returns a structure containing information on any error messages. It has the following fields (note that the order of the fields in the structure is important when creating and adding the fields to the structure):
Field Name |
Type |
Length |
TYPE |
CHAR |
1 |
CODE |
CHAR |
5 |
MESSAGE |
CHAR |
220 |
LOG_NO |
CHAR |
20 |
LOG_MSG_NO |
NUMC |
6 |
MESSAGE_V1 |
CHAR |
50 |
MESSAGE_V2 |
CHAR |
50 |
MESSAGE_V3 |
CHAR |
50 |
MESSAGE_V4 |
CHAR |
50 |
The internal names of the fields of both the table and the structure are the same as the name of the parameter.
Java RFC Code for Making this RFC Function Call
The following code
uses a properties file for setting connection information. The code creates the IRfcModule object manually.// Obtain an instance of the session manager
SessionManager sessionMgr = SessionManager.getInstance();
// Start the session:
SessionMgr.open();
// Get a module factory object
IRfcModuleFactory moduleFactory = sessionMgr.getRfcModuleFactory();
// **** Manually Create the IRfcModule object ****
IRfcModule rfcModule = moduleFactory.createRfcModule("BAPI_GL_ACC_GETLIST");
// Obtain the factories for creating parameters
ISimpleFactory simpleFactory = sessionMgr.getSimpleFactory();
IStructureFactory structureFactory = sessionMgr.getStructureFactory();
ITableFactory tableFactory = sessionMgr.getTableFactory();
// Create the import parameter(s) of the function module
ISimple paramCompany = simpleFactory.createSimple(
new SimpleInfo(null, IFieldInfo.RFCTYPE_CHAR, 4, 0),
"COMPANYCODE");
ISimple paramLanguage = simpleFactory.createSimple(
new SimpleInfo(null, IFieldInfo.RFCTYPE_CHAR, 1, 0),
"LANGUAGE");
ISimple paramLanguageIso = simpleFactory.createSimple(
new SimpleInfo(null, IFieldInfo.RFCTYPE_CHAR, 2, 0),
"LANGUAGEISO");
// Add the import parameters to the function module
rfcModule.addImportParam(paramCompany);
rfcModule.addImportParam(paramLanguage);
rfcModule.addImportParam(paramLanguageIso);
// Create the table parameter(s) of the function module
IFieldInfo[] tableColumns = {
new SimpleInfo("COMP_CODE", IFieldInfo.RFCTYPE_CHAR, 4, 0),
new SimpleInfo("GL_ACCOUNT", IFieldInfo.RFCTYPE_CHAR, 10, 0),
new SimpleInfo("LONG_TEXT", IFieldInfo.RFCTYPE_CHAR, 50, 0),
new SimpleInfo("SHORT_TEXT", IFieldInfo.RFCTYPE_CHAR, 20, 0),
};
ComplexInfo tableMetaData = new ComplexInfo(tableColumns, null);
ITable tableAccountList = tableFactory.createTable(tableMetaData, "ACCOUNT_LIST");
//An alternative way of creating the table parameter,
// which automatically creates the columns of the table:
//tableFactory.autoCreateTable("ACCOUNT_LIST", "BAPI3006_1");
// Add the table parameter(s) to the function module
rfcModule.addTableParam(tableAccountList);
// Create the export parameter(s) of the function module
IFieldInfo[] returnFieldTypes = {
new SimpleInfo("CODE", IFieldInfo.RFCTYPE_CHAR, 5, 0),
new SimpleInfo("LOG_MSG_NO", IFieldInfo.RFCTYPE_NUM, 6, 0),
new SimpleInfo("LOG_NO", IFieldInfo.RFCTYPE_CHAR, 20, 0),
new SimpleInfo("MESSAGE", IFieldInfo.RFCTYPE_CHAR, 220, 0),
new SimpleInfo("MESSAGE_V1", IFieldInfo.RFCTYPE_CHAR, 50, 0),
new SimpleInfo("MESSAGE_V2", IFieldInfo.RFCTYPE_CHAR, 50, 0),
new SimpleInfo("MESSAGE_V3", IFieldInfo.RFCTYPE_CHAR, 50, 0),
new SimpleInfo("MESSAGE_V4", IFieldInfo.RFCTYPE_CHAR, 50, 0),
new SimpleInfo("TYPE", IFieldInfo.RFCTYPE_CHAR, 1, 0),
};
ComplexInfo returnType = new ComplexInfo(returnFieldTypes, null);
IStructure paramReturn = structureFactory.createStructure(returnType, "RETURN");
//An alternative way of creating the structure parameter,
// which automatically creates the fields of the structure:
//structureFactory.autoCreateStructure("RETURN", "BAPIRETURN");
// Add the export parameter(s) to the function module
rfcModule.addExportParam(paramReturn);
// **** End of Manually Creating the IRfcModule object ****
// An alternative way for creating the IRfcModule object,
// which automatically creates and adds all of its parameters:
// IRfcModule rfcModule = moduleFactory.autoCreateRfcModule("BAPI_GL_ACC_GETLIST");
// Set the values of the import parameters
paramCompany.setString("3000");
paramLanguage.setString("E");
try
{
// Call the RFC function
int retCode = rfcModule.callReceive();
// Print out values of various parameters
System.out.println("BAPI_GL_ACC_GETLIST return code = " + retCode);
System.out.println("=================================");
for (int i = 0; i < rfcModule.getExportParamCount(); i++)
{
System.out.print(rfcModule.getExportParam(i));
}
for (int j = 0; j < rfcModule.getTableParamCount(); j++)
{
ITable table = rfcModule.getTableParam(j);
System.out.println("Showing table " + j);
System.out.print(rfcModule.getTableParam(j));
System.out.println();
}
for (int k = 0; k < rfcModule.getImportParamCount(); k++)
{
System.out.println("Showing import param " + k);
System.out.print(rfcModule.getImportParam(k));
System.out.println();
}
}
// Handle errors
catch (JRfcRfcAbapException e)
{
e.printStackTrace();
}
catch (JRfcRemoteServerException e)
{
e.printStackTrace();
}
catch (JRfcRfcConnectionException e)
{
e.printStackTrace();
}