Open all | Close all

Getting the LATEST information!

To ensure you are getting the latest information please vist this page on the new domain for SAP Development


Example JCo code to call SAP function module


Example code which calls an SAP function module 'BAPI_MATERIAL_GETLIST' with inport parameters and retrieves the resultant
data.

package ws;  //Comment out or replace with own package declaration

import com.sap.mw.jco.*;

//import javax.swing.*;          //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
				//Swing releases before Swing 1.1 Beta 3.
//import java.awt.*;
//import java.awt.event.*;
import java.util.*;

public class CallSapFunc extends Object {
	public ArrayList myArrList = new ArrayList();
	//
	int count;
	String[] SAPInterfaces;
	JCO.Client mConnection;
	JCO.Repository mRepository;
	public CallSapFunc(){
		try {
			// Logon info
			mConnection = JCO.createClient(	"500", // SAP client
					"Username", // userid
					"password", // password
					null, // language
					"Server name", // application server host name
					"00"); // system number
			mConnection.connect();
			mRepository = new JCO.Repository("ARAsoft", mConnection);
		} catch (Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}

		JCO.Function function = null;
		JCO.Table codes = null;
		JCO.Table codes2 = null;

		try {
			function = this.createFunction("BAPI_MATERIAL_GETLIST");


			if (function == null) {
				System.out.println(
					"BAPI_MATERIAL_GETLIST" + " not found in SAP.");
				System.exit(1);
			}

			codes = function.getTableParameterList().getTable("MATNRSELECTION");

			codes.appendRows(2);   // Add two rows to internal table
			codes.setValue("I", "SIGN");
			codes.setValue("EQ", "OPTION");
			codes.setValue("P1001087", "MATNR_LOW");			

			codes.nextRow();      // Move onto next row
			codes.setValue("I", "SIGN");
			codes.setValue("BT", "OPTION");
			codes.setValue("P1001087", "MATNR_LOW");	
			codes.setValue("P1009999 ", "MATNR_HIGH");			

			mConnection.execute(function);
			codes2 = function.getTableParameterList().getTable("MATNRLIST");
			
			for (int loop = 0; loop < codes2.getNumRows(); loop++){
				myArrList.add(codes2.getValue("MATERIAL"));
				codes2.nextRow();
			}

			if (myArrList.isEmpty()) {
 			 	System.out.println("No data retirved for selection criteria");
				System.exit(1);
			}		
		} catch (Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}
	}

	public JCO.Function createFunction(String name) throws Exception {
		try {
			IFunctionTemplate ft =
				mRepository.getFunctionTemplate(name.toUpperCase());
			if (ft == null)
				return null;
			return ft.getFunction();
		} catch (Exception ex) {
			throw new Exception("Problem retrieving JCO.Function object.");
		}
	}
}