Show TOC

Calling BAPIs from JavaLocate this document in the navigation structure

Use

This is an example program for calling a BAPI from the IBM development platform, Access Builder.

More detailed example programs are delivered with the Access Builder.

Access Builder

//Importing Required Classes:
import com.sap.rfc.*; 
import com.sap.rfc.exception.*; 
import com.ibm.sap.bapi.*;
import com.ibm.sap.bapi.generated.*;
//Connecting to the SAP System:
static private IRfcConnection establishConnection(MiddlewareInfo aMiddlewareInfo)
   throws JRfcRemoteException
{
   IRfcConnection aConnection = null ;
   ConnectInfo aConnectInfo = null ;
   UserInfo aUserInfo = null ;
   String orbServerName = aMiddlewareInfo.getOrbServerName() ; 
   // Please adjust the values written in UPPERCASE LETTERS
   // in the lines below so that they fit to your needs!
   // If you don't know the correct values ask your system 
   // administrator!
   // After correcting these values you should change the 
   // <bAdjusted> variable in the following line
   // from "false" to "true".
   // Then you can re-compile ("javac SampleCompanyCode.java") and 
   // re-run ("java SampleCompanyCode -conn JNI") this sample...
   boolean bAdjusted = true;
   if (!bAdjusted) {
      throw (new JRfcRfcConnectionException (
         "Please adjust the Connection-Parameters to your 
                 needs! (See method \"establishConnection\")"));
   }  
    //Connection Information: 
   aConnectInfo = new ConnectInfo (
      3,    // int aRfcMode 3=R/3 or 2=R/2
      null,    // String aDestination
      "9.7.12.7", // String aHostName YOUR  HOSTNAME (e.g. IP- 
                     //address)
      0,      // int aSystemNo YOUR SYSTEM-NUMBER
      null,          // String aGatewayHost
      null,          // String aGatewayService
      null,          // String aSystemName
      null,          // String aGroupName
      null,          // String aMsgServer
      false,         // Boolean isLoadBalancing
      true);         // Boolean isCheckAuthorization
    //User Information:
   aUserInfo = new UserInfo ( 
      "MUSTER",      // String aUserName,    YOUR USERID
      "IDES",     // String aPassword,    YOUR PASSWORD
      "800",      // String aClient, YOUR CLIENT NUMBER
      "e",        // String aLanguage, YOUR PREFERRED 
                            //LANGUAGE
      1103);      // int aCodePage YOUR REQUIRED CODEPAGE   
    //Technical Conversion for the Selected Middleware;
    // Open Connection:
   IRfcConnectionFactory aConnectionFactory = FactoryManager.getSingleInstance().getRfcConnectionFactory() ;
   aConnection = aConnectionFactory.createRfcConnection(aConnectInfo, aUserInfo) ;
   aConnection.open() ;
   //Zurückgeben der Verbindung:
   return aConnection ;
}
//Calling the Main Method:
public static void main (java.lang.String[] args)
  //Setting up the Connection Using Selected Middleware: 
{
   MiddlewareInfo aMiddlewareInfo = new MiddlewareInfo(args) ;
   FactoryManager aFactoryManager = FactoryManager.getSingleInstance() ;
   aFactoryManager.setMiddlewareInfo(aMiddlewareInfo) ;
    //Initializing the Connection Object:    
   IRfcConnection aConnection = null ;
   try
   {
      aConnection = establishConnection(aMiddlewareInfo) ;
   }
   catch (Exception ex) 
   {
      System.out.println("ERROR : Could not create connection : " + ex) ;
      System.exit(-1) ;
   }  
   System.out.println("Connection established.");
   // --- TEST CODE (start) --------------------------------------
   try
   {
      printList(aConnection) ;
//Calling the BAPI: 
  //Declaring an Empty Object ID for Business Object
  //CompanyCode:     
      objectId = CompanyCode.getEmptyObjectId() ;
   //Entering a Value in the Object ID:   
      objectId.getKeyField("COMPANYCODEID").setString("1000") ;
   //Instantiating the Object CompanyCode with the Object ID:
      companyCode = new CompanyCode(objectId) ; // Create 2nd 
         CompanyCode
      System.out.println ("Successfully created new CompanyCode : '" + companyCode + "'") ;
      printDetails(companyCode, aConnection) ;
   }  
   // --- TEST CODE (end) ----------------------------------------
   catch (Exception ex)
   {
      System.out.println ("Unexpected exception occurred:");
      System.out.println (ex);
   }
}
private static void printDetails(CompanyCode companyCode, IRfcConnection connection) 
{
   try   
   {
    //Declaring the Parameters of the BAPI CompanyCode.GetDetail:
      CompanyCodeGetdetailParams aCompanyCodeGetdetailParams = 
         new CompanyCodeGetdetailParams() ;
    //Calling BAPIs CompanyCode.GetDetail on Object Instance:
      companyCode.getdetail(connection,   aCompanyCodeGetdetailParams);
      //Splitting the Parameter Objects into its Separate Components
      //(Structure):
      Bapi0002_2Structure struct = aCompanyCodeGetdetailParams.getCompanycodeDetail() ;
      System.out.println ("The details of the companycode are : ") ;
      //Splitting the Structure into Individual Fields:     
      System.out.println ("CompCode :           '" + struct.getCompCode()  + "'");
      System.out.println ("CompName :           '" + struct.getCompName()  + "'");
      System.out.println ("City1 :              '" + struct.getCity()  + "'");
      System.out.println ("Country1 :           '" + struct.getCountry() + "'");
      System.out.println ("Currency :           '" + struct.getCurrency() + "'");
      System.out.println ("Langu1 :             '" + struct.getLangu() + "'");
      System.out.println ("ChrtAccts :          '" + struct.getChrtAccts() + "'");
      System.out.println ("FyVariant :          '" + struct.getFyVariant() + "'");
      System.out.println ("VatRegNo :           '" + struct.getVatRegNo() + "'");
      System.out.println ("Company :            '" + struct.getCompany() + "'");
      System.out.println ("AddrNo :             '" + struct.getAddrNo() + "'");  
      System.out.println() ;
   }
   catch (Exception ex) 
   {
      System.out.println("Exception in printDetails() : " + ex) ;
   }  
   return;
}
private static void printList(IRfcConnection connection) 
{
   try   
   {
      //Declaring the Parameter Object:      
      CompanyCodeGetlistParams aCompanyCodeGetlistParams = 
         new CompanyCodeGetlistParams() ;
      //Actual BAPI Call:
      CompanyCode.getlist(connection, aCompanyCodeGetlistParams);
      //Splitting the Parameter Objects into its Separate Components
      //(Table):
      Bapi0002_1Table table = aCompanyCodeGetlistParams.getCompanycodeList();
      int rowCount = table.getRowCount() ;
      System.out.println ("Returned table has " + rowCount + " lines.");
      //Evaluating the Table Row-by-Row: 
      for (int i = 0; i < rowCount; i++)
      {
         Bapi0002_1TableRow row = table.getRow(i) ;
         System.out.println("\t" + row.getCompCode() + "\t" + row.getCompName()) ;
      }
      System.out.println() ;
   }
   catch (Exception ex) 
   {
      System.out.println("Exception in printList() : " + ex) ;
   }  
   return;
}
}