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; //Replace with name of your package

import com.sap.mw.jco.*;

public class CallFunction extends Object {

	public static void main(String args[]) {
		CallFunction app = new CallFunction();
	}
	int count;
	JCO.Client mConnection;
	JCO.Repository mRepository;
	String[] SAPInterfaces;
	public CallFunction() {
		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;
		//		codes.firstRow();

		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.setValue("", "MATNR_HIGH");

			codes.nextRow(); // Move onto next row
			codes.setValue("I", "SIGN");
			codes.setValue("EQ", "OPTION");
			codes.setValue("P1001088", "MATNR_LOW");
			codes.setValue("", "MATNR_HIGH");

			mConnection.execute(function);

//			JCO.Table returnTable1 =
//				function.getTableParameterList().getTable("MATERIALSHORTDESCSEL");
//			System.out.println(returnTable1);

//			JCO.Table returnTable2 =
//				function.getTableParameterList().getTable("MATNRLIST");
//			System.out.println(returnTable2);

			// Output result Material list table
			System.out.println(
				function.getTableParameterList().getValue("MATNRLIST"));
			System.out.println(
				function.getTableParameterList().getString("MATNRLIST"));

//			System.out.println(
//				function.getTableParameterList().getValue("MATERIALSHORTDESCSEL"));
//
//			System.out.println(
//				function.getTableParameterList().getString("MATERIALSHORTDESCSEL"));

			// Output Selection table
			System.out.println(
				function.getTableParameterList().getValue("MATNRSELECTION"));
			System.out.println(
				function.getTableParameterList().getString("MATNRSELECTION"));

		} 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.");
		}
	}
}