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.










Saturday, 31 January 2015

Connect to SAP using Java (JCO)

Hi Dear Viewers, 


If you have landed on my blog, reading this page - probably you might have tasted the bitterness in connecting/integrating SAP with Java. My past experience also gave me this sourness while I was trying to connect SAP with my Java platform. So, this blog is just to share my learning during the entire process of successful connection with SAP using Java connector (JCo). 

Let's get in to details now....

Before you start with your coding, ensure you setup the environment for SAPJCo without which your coding will not be recognised by the environment. Let's talk about Windows environment here. 

Environment Setup
  • Download the SAPJCo library files from SAP market place.
  • Make sure, you find sapjcorfc.dll and librfc32.dll files in the downloaded version.
  • Copy sapjcorfc.dll and librfc32.dll to the System32 folder i.e., C:\Windows\System32.
  • Also, cross check whether your C:\Windows\System32 has the files MSCVR71.dll and MSCVP71.dll. If not, copy and put it there.
  • Now, copy sapjco.jar and sapjcorfc.dll in to the ext folder of your JRE i.e.,  ...\Java\jrex\lib\ext folder (x in jrex is your Java version - 6 or 7...).
  • Now, the last step is to add sapjco.jar to the buildpath of your project folder.
Cool... you are ready with your environment setup. Now, let's take a look how to connect to the SAP repository with Java code. 

Connect to SAP with Java class
  • IFunctionTemplate, IRepository and JCO are very important classes for connecting to SAP environment. 
          Import below classes in your Java class:
                       import com.sap.mw.jco.IFunctionTemplate;
                       import com.sap.mw.jco.IRepository;
                       import com.sap.mw.jco.JCO;
  • Now, add a connection pool to the intended SAP system. You need to use the method JCO.addClientPool(....) with the SID, max no. of connections, sap client id, credentials to the system i.e., user name/password, language, host and system number. Get the required information from your operating team and pass them to this method addClientPool(....) as arguments.
  • Next step is a create a repository using the SID (from above point) i.e., JCO.createRepository("xxxx", SID) and assign it to the repository object i.e., IRepository repo = JCO.createRepository("xxxx", SID).
  • Add some logger messages so that you know what's going on in between the processing.
  • Make sure you handle all the JCO exceptions (eg., class JCO.Exception) during the process.
  • Make an important note that whatever connection pool you are creating should be released once the intended functionality with the code is done after connecting, otherwise your system will go for a toss. You can cleanup/destroy the connection pool by using removeClientPool(..) method - passing your SID as argument i.e.,  JCO.removeClientPool(SID);


Bravo, you have done an excellent job. 



Knowledge grows by sharing... as I shared my learning with you, please share your learning with others... so that, others can benefit from free knowledge :)

Regards,