The Basics of the BAPI Call

Team-Fly

Most communication between a Java program and an SAP system is done through BAPIs. As discussed in Chapter 18, the basic construct for calling a BAPI is as follows:

call function ‘Scooby-doo'     exporting         Parameters     Importing         Parameters     Tables         Parameters     Exceptions         Parameters.

After the link to the SAP system is established through the IRfcConnection factory, you need the ability to set up the export and table variables, make the call to the SAP system, and then read in the import and table variables. You also need the ability to handle any exceptions that occur. All activity with the BAPI is handled through the IRfcModule Java interface.

Handling Simple Import and Export Parameters

Let's start by looking at how to populate a simple export variable (import variables are set up in an identical fashion). In other words, the export parameter for the BAPI comprises one of the basic SAP data element types, such as CHAR or NUM, and is not a structure. The export variable can be set up manually through a call to the addExportParam method of the IRfcModule object. This method expects an object containing the necessary information about the export parameter, such as data type, parameter name, and value. For a simple variable, this information is contained in an object called ISimple. The following code segment summarizes this entire process.

// Instantiate the Isimple Class ISimple simpleParam; // Instantiate the Factory Manager Class FactoryManager facMan = FactoryManager.getSingleInstance(); // Instantiate the IsimpleFactory ISimpleFactory simpleFac = facMan.getSimpleFactory();// Create the Isimple object and reference it to simpleParam simpleParam = simpleFac.createSimple(  new SimpleInfo(null, IFieldInfo.RFCTYPE_CHAR, 10, 0), "KUNNR"); // Instantiate the IrfcModuleFactory and IRfcModule IRfcModuleFactory moduleFac = facMan.getRfcModuleFactory(); IRfcModule theModule = moduleFac.createRfcModule(connection, "RFCNAME"); // Set the value for the variable theModule.setSimpleExportParm("KUNNR").setString("Prima"); // Assign the Export Parameter theModule.addExportParam(simpleParam);
Tip 

Do not let the constructor for Isimple-SimpleInfo(null, IFieldInfo._RFCTYPE_CHAR, 10, z)-intimidate you. All the parameters for constructors and most methods are covered in Chapters 19 and 20. In this case, the first parameter is used if this field is part of a structure, the second is the data type, the third is the data length, and the fourth is the number of decimals.

In the preceding code segment, the constructor for SimpleInfo defines the values such as data type and size, and the call to the simple factory ties this object to an actual parameter in the RFC, in this case KUNNR. This method of creating the export parameter works if you know the parameter definitions of the BAPI and you want to hard code these into your Java program. However, you can also build this export parameter automatically, via the getSimpleInfo method of the IRfcModule interface. Using the automatic creation mode, the same example can be coded as follows.

// Instantiate the Isimple Class ISimple simpleParam; // Instantiate the Factory Manager Class FactoryManager facMan = FactoryManager.getSingleInstance(); // Instantiate the IsimpleFactory ISimpleFactory simpleFac = facMan.getSimpleFactory(); // Instantiate the IrfcModuleFactory and IRfcModule IRfcModuleFactory moduleFac = facMan.getRfcModuleFactory(); IRfcModule theModule = moduleFac.createRfcModule(connection, "RFCNAME"); // Get the values for the variable ISimple simpleParam = theModule.getSimpleImportParam("KUNNR"); // Set the value for the variabletheModule.setSimpleExportParm("KUNNR").setString("Prima"); // Assign the Export Parameter theModule.addExportParam(simpleParam);

As you can see, this approach is a little less cumbersome to use than the original, and no knowledge is required about the parameter that is to be defined. You can view or use the current value of either export or import parameters via methods of the ISimple interface, such as getFloat() or getString(). These methods return the value of the variable in the format specified (for example, getFloat() returns a float).

With the basics of simple import and export parameters out of the way, we can move on to perform the RFC itself.

Calling the Remote Function Module

The remote function module to be called by a Java program is defined through the methods autoCreateRfcModule or CreateRfcModule of the IRfcModuleFactory. The difference between the methods is in how the parameters are created. The autoCreateRfcModule automatically creates all parameters, whereas the constructor for CreateRfcModule allows you to create none, some, or all. If you are going to use all of the parameters in a BAPI call, then use the autoCreateRfcModule. Examples for both methods follow, and the full syntax for both is discussed in Chapter 19.

// Autocreate example IRfcModule theModule = moduleFac.autoCreateRfcModule(connection, "RFCNAME"); // Create example IRfcModule theModule = moduleFac.createRfcModule(connection, "RFCNAME"); theModule.addImportParam(simpleParam); theModule.addExportParam(simpleParam);

After the IRfcModule object has been created and the export variables have been defined with values, the call to the RFC itself is accomplished through one of three methods:

  • Call(). A one-way call to the SAP system. Use this method when you are not receiving any values (including tables) from the function module.

  • Receive(). A one-way call from the SAP system. Use this method when you are not sending any values to the function module.

  • CallReceive(). A two-way call to and from the SAP system. Use this method when you are both sending and receiving values from the function module.

You can also remove fields from the IRfcModule object.

Handling Complex Import and Export Variables

Import and export parameters are often not simple variables, but instead are structures composed of many simple fields.

Examples of setting up structures both manually and automatically follow.

// Example of manual creation of structure IStructure structParam; IStructureFactory structFac = facMan.getStructureFactory(); structParam = structFac.createStructure(); SimpleInfo simpleInfos[] =  { new SimpleInfo("KUNNR", IFieldInfo.RFCTYPE_CHAR, 10, 0), new SimpleInfo("ANRED", IFieldInfo.RFCTYPE_CHAR, 15, 0), …… }; ComplexInfo complexInfo = new ComplexInfo(simpleInfos, null); structParam.setComplexInfo(complexInfo); structParam.setParameterName("PARAMNAME"); // Example of Automatic Creation of Structure IStructure structParam; IStructureFactory structFac = facMan.getStructureFactory(); IRfcConnection connection = …; connection.open(); structParam = structFac. autoCreateStructure("PARAMNAME", connection, "TABNAME");

Handling Tables and Cursors

Defining tables is very similar to defining complex info objects and is done through the objects ITable and IRow.

Sample code for building table entries follows.

// Sample code for adding a row to an iTable object ITable theTable = …; IRow theRow = theTable.createEmptyRow(); theRow.getSimpleField("KUNNR").setString(custNo); theRow.getSimpleField("NAME1").setString(custName); theTable.appendRow(theRow); // Sample code for updating a row  ITable theTable = …; IRow rowCopy = theTable.getRow(0); //get a copy of the first row rowCopy.getSimpleField("KUNNR").setString(custNo);  rowCopy.getSimpleField("NAME1").setString(custName); theTable.updateRow(0, rowCopy);

Exceptions and Errors

You must be concerned with two types of errors when making the BAPI calls: catchable Java errors and the parameter RETURN, which is associated with all BAPIs. To write bulletproof code, you should always set up your program to catch all Java errors and to test the RETURN parameter. Information on all catchable errors for all methods is provided in Chapters 19 and 20. You can also set up your Java compiler to catch all exceptions.


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net