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.
- 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.
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.
No comments:
Post a Comment