Thursday, 12 February 2015

How to update data through RFC using JCo in SAP?

Before updating the data in SAP, we need to have a working connection to SAP.

Please refer my previous blog posts for connecting to SAP & retrieving data from SAP so that you get an overview of SAP connection basics and function templates for the CRUD operations.

Follow the first 4 steps as specified in the post retrieving data from SAP i.e., from imports till getting client object JCO.Client client = JCO.getClient(SID);

Next step is to set the criteria of the fields wrt the table for which we are updating the data. Note that Field is a class which need to be imported i.e.,      
 import com.sap.mw.jco.JCO.Field;
    .................
    .................
Field fieldname1 = jcoFunction.getImportParameterList().getField("FIELD_NAME_XXX");
fieldname1 .setValue("123");

If  you have more than one field criteria, you need to repeat the above two lines for as many fields as you want to set.
Note that FIELD_NAME_XXX is your actual field name specification & instead of 123, you need to set your own criteria.

Once the fields are set, just set all the column values for the required table with appropriate data i.e.,
JCO.Table YOUR_TABLE_NAME = function.getTableParameterList().getTable("TABLE_NAME_XXX");
YOUR_TABLE_NAME.appendRow();
YOUR_TABLE_NAME.setValue("111", "FIELD_NAME_XXX");
YOUR_TABLE_NAME.setValue("222", "FIELD_NAME_1");

YOUR_TABLE_NAME.setValue("333", "FIELD_NAME_2");
    .................
    .................

Then execute the function using the client object
  client.execute(function);

Executing function doesn't mean that your new data is updated to the row, it's only set. Now, you need to commit the data as we do in our database transactions.

Inorder to commit, we need to first get the function template of commit function available in SAP repository i.e.,
IFunctionTemplate commitfunctemplate = repository.getFunctionTemplate("BAPI_TRANSACTION_COMMIT");

Declare a function variable, say JCO.Function commitVar;

Using this variable commitVar, create your function template i.e., 
commitVar = new JCO.Function(commitfunctemplate);

Finally execute the commit function after a clean null check i.e.,
if (commitVar == null)
throw new Exception();
client.execute(jcoCommit);

That's it, your data is updated to SAP. 
Incase you want to check the return structure you can do this by using
JCO.Structure returnStruct = function.getExportParameterList().getStructure("RETURN");


As stated in my previous post, do not forget to release the client object & remove the connection pool. Needless to mention, handle exceptions wherever applicable & required.

Enjoy with your data updates :)

Thank you.












Friday, 6 February 2015

How to retrieve data from SAP through RFC using JCo?

Once you have done the environment setup and are ready with connection to SAP programmatically, the next task is to retrieve/insert/update data to the SAP system using RFCs according to your business needs.

In this blog, we will take a closer look at retrieving the data. For connecting to SAP please refer my previous blog post.

Let's start step by step:

  • Make sure you have the following imports in your Java class:

         import com.sap.mw.jco.IFunctionTemplate;
         import com.sap.mw.jco.JCO;

  • Get the function template from the sap repository i.e., 

         IFunctionTemplate functemplate = repo.getFunctionTemplate("xxxxx");
         Replace "xxxxx" with the actual function definition name from your 
         sap repository.

  • If the function definition was found in the SAP backend, then create a function from the template object i.e., 
        ......
        ......
        if (functemplate != null) {
           JCO.Function function = functemplate.getFunction();


  • Now, you have to get the client object from the pool, this is important because you need to execute the function with the help of this client.
         JCO.Client client = JCO.getClient(SID);


  • If there are any input parameters that need to be passed for criteria/querying the table then you need to set that parameters otherwise, you can skip this step.
         JCO.ParameterList inputparams = function.getImportParameterList();
         inputparams.setValue("val1", "PARAM_NAME_1"); 
         inputparams.setValue("val2", "PARAM_NAME_2");
         ......
         ......


  • Execute the remote function with the help of client i.e., 

         client.execute(function);

  • Using function.getExportParameterList(), you can check the return JCO structure.
  • Next step is to get the table for fetching the data i.e., 

         JCO.Table mytable = function.getTableParameterList().getTable("xxxxx");
         Replace "xxxxx" with the actual table name from the sap backend.

  • Next, just print the results to the console using JCO.Table object's fields (iterating over all the fields) i.e.,

         if (mytable.getnumrows() > 0)
         {
           do {
                 for (JCO.FieldIterator e = mytable.fields(); e.hasMoreElements();) {
JCO.Field field = e.nextField();
System.out.println(field.getName() + ":\t" + field.getString());
                }
             } while (mytable.nextRow());
} else {
System.out.println("No data found");
        }

  • Make sure, you catch exceptions where ever required in the above process.

  • Finally, release the client object using

          JCO.releaseClient(client);

Also, as stated in the previous post, do not forget to remove the connection pool.

Hope this step by step procedure helped you to get the required information on retrieval of data through RFC using JCo. Cheers :)

Thank you.