Best Information For Talk to SAP with JCo | Talk to SAP with JCo Articles
To create a Java-based Web Service that relies on SAP data and functionality we will need a mechanism for communicating with SAP. That mechanism is JCO. This article is not a complete tutorial on JCO. More information about JCo is available at ftp://ftp.sap.com/pub/sapjco/ and downloading jco-docs-1.1.zip. Unzip this file and view the file JCOTutorial.pdf in the docs directory.
In this article we will demonstrate how to use JCO to do the following:1. Connect with SAP2. Create a respository for BAPI metadata3. Set BAPI input parameters4. Execute the BAPI5. Extract BAPI output parameters6. Disconnect from SAP
Connect to SAPThe JCO.createClient() method uses SAP connection information to create a JCO.client object. Then the connect() attempts to login to R/3 using a specific userid and a password. If successful, the JCO.client object will represent an SAP connection.JCO.Client client = JCO.createClient( look at docs for input parameters ); client.connect();
BAPI Repository and metadataA BAPI Repository is a place to store BAPI metadata. This is information about the BAPI itself including all input, output, and in/out parameter names, data types, and lengths.JCO.Repository repository = new JCO.Repository("Gamma", client);
We now ask the repository for metadata about a specific BAPI. Here we are working with the BAPI_MATERIAL_GETLIST. Please review SAP documentation for more information about this BAPI. This BAPI actually uses 10 different parameters. By asking the repository for BAPI metadata, we do not have to write the code to set up these 10 parameters. Also, we are returned a JCO.Function object that contains BAPI metadata.
JCO.Function function = repository.getFunctionTemplate("BAPI_MATERIAL_GETLIST").getFunction();
Setting input parametersBefore we execute this BAPI, we must set some input parameters. We will set three fields within the MATNRSELECTION table parameter, MATNR_LOW – this will contain a wildcard pattern to search for materials by name. For example, vac* will return all materials starting with the letters vac.SIGN – set to I to include materials.
OPTION – set to CP to indicate that MATNR_LOW contains a pattern.
First we need to get the table parameter into a JCO.Table object.
JCO.Table matSelTable = function.getTableParameterList().getTable("MATNRSELECTION");
Now we append a row to this empty table and add values to these fields.
Executing the BAPI
Now that the function object's input parameters have been initialized, we can execute the BAPI with out JCO.client object.