Skip to Content

I hope you’ve read the first Parts of this Weblog series:

Finally I close this Weblog Series with a JCo code example. This example creates a Business Partner and read this newly created BP.

logon.properties

To make it easier for you to adopt your login information I’ve set up an external logon.properties file. This is the content:

jco.client.client=002
jco.client.user=developer
jco.client.passwd=XXXXXXXX
jco.client.ashost=gateway
jco.client.sysnr=00
jco.client.lang=EN
jco.client.abap_debug=0
jco.client.use_sapgui=0

With the Property jco.client.abap_debug you have the possibility to debug the function calls. But SAP GUI has to be installed on the system where you execute the code.

BupaCreate.java

This is the example Code using the SAP Java Connector:

import com.sap.mw.jco.*;
 
public class BupaCreate {
 
  // Name of the ClientPool
  static final String POOL_NAME = "Pool";
  JCO.Client mConnection;
 
  // The repository we will be using
  IRepository repository;
 
  public BupaCreate()
  {
    try {
      JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);
      if (pool == null) {
        OrderedProperties logonProperties =
          OrderedProperties.load("/logon.properties");
        JCO.addClientPool(POOL_NAME,  // pool name
                          5,          // maximum number of connections
                          logonProperties);  // properties
      }
      mConnection = JCO.getClient(POOL_NAME);
      // Create a new repository
      repository = JCO.createRepository("MYRepository", POOL_NAME);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
 
  // Retrieves and prints information about the remote system
  public void BupaCreateFromData()
  {
    try {
 
      // Get a function template from the repository
      IFunctionTemplate ftemplate =
        repository.getFunctionTemplate("BAPI_BUPA_CREATE_FROM_DATA");
      IFunctionTemplate ftemplatecommit =
        repository.getFunctionTemplate("BAPI_TRANSACTION_COMMIT");
      IFunctionTemplate ftemplateread =
        repository.getFunctionTemplate("BAPI_BUPA_CENTRAL_GETDETAIL");
 
      // if the function definition was found in backend system
      if(ftemplate != null) {
 
        // Create a function from the template
        JCO.Function function = ftemplate.getFunction();
        JCO.Function commit   = ftemplatecommit.getFunction();
        JCO.Function read     = ftemplateread.getFunction();
       
        // Set up Import Parameters
        JCO.ParameterList imports = function.getImportParameterList();
        JCO.Structure centralDataPerson =
          imports.getStructure("CENTRALDATAPERSON");
        centralDataPerson.setValue("Gregor","FIRSTNAME");
        centralDataPerson.setValue("Wolf","LASTNAME");
       
        function.getImportParameterList().setValue("1", "PARTNERCATEGORY");
 
        // Get a client from the pool
        JCO.Client client = JCO.getClient(POOL_NAME);
 
        // We can call 'RFC_SYSTEM_INFO' directly since it does
        // not need any input parameters
        client.execute(function);
       
        // Get table containing the return
        JCO.Table returntab =
          function.getTableParameterList().getTable("RETURN");
   
        // Print results
        if (returntab.getNumRows() > 0) {
   
          // Loop over all rows
          do {
   
            System.out.println("-----------------------------------------");
   
            // Loop over all columns in the current row
            for (JCO.FieldIterator e =
                   returntab.fields(); e.hasMoreElements();) {
              JCO.Field field = e.nextField();
              System.out.println(field.getName() + ":\t" + field.getString());
            }//for
          } while(returntab.nextRow());
        }
        else {
          System.out.println("No Errors in the RETURN Table found");
          // Read new BUSINESSPARTNER Number
          String bpNumber =
            function.getExportParameterList().getString("BUSINESSPARTNER");
         
          System.out.println("New Business Partner Number: " + bpNumber);
          // Commit
          client.execute(commit);
          // Set
          read.getImportParameterList().setValue(bpNumber, "BUSINESSPARTNER");
          // Commit
          client.execute(read);
          // Read Export
          JCO.ParameterList readExport = read.getExportParameterList();
          JCO.Structure readCentralDataPerson =
            readExport.getStructure("CENTRALDATAPERSON");
          System.out.println("Firstname: " +
            readCentralDataPerson.getString("FIRSTNAME"));
          System.out.println("Lastname : " +
            readCentralDataPerson.getString("LASTNAME"));
        }//if
       
        // Release the client into the pool
        JCO.releaseClient(client);
      }
      else {
        System.out.println(
          "Function BAPI_BUPA_CREATE_FROM_DATA not found in backend system.");
      }
    }
    catch (Exception ex) {
      System.out.println("Caught an exception: \n" + ex);
    }
 
  }
  protected void cleanUp() {
    JCO.removeClientPool(POOL_NAME);
  }
 
  public static void main(String[] argv)
  {
    BupaCreate e = new BupaCreate();
    e.BupaCreateFromData();
    e.cleanUp();
  }
}
To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Stefan Klensch
    Hi Gregor,
    i guess your aim was to show ways to develop JCo applications using the JCo API and also without having a NetWeaver Developer Studio available.
    But for people “owning” the NWDS i strongly recommend to use the “Import RFC Modules from R/3” feature which 1. helps you locating BAPIs/RFCs in a system and 2. generates type-safe proxies, which are much easier to use and not that error-prone than the generic JCo API usage you described.
    Best regards,
    Stefan
    (0) 

Leave a Reply