jco code to call sap function module
TRANSCRIPT
![Page 1: JCo code to call SAP function module](https://reader036.vdocument.in/reader036/viewer/2022082318/551cfd9649795964198b4792/html5/thumbnails/1.jpg)
http://www.sapdev.co.uk/java/jco/jco_callfunc.htm
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 numbermConnection.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);}
![Page 2: JCo code to call SAP function module](https://reader036.vdocument.in/reader036/viewer/2022082318/551cfd9649795964198b4792/html5/thumbnails/2.jpg)
codes = function.getTableParameterList().getTable("MATNRSELECTION");
codes.appendRows(2); // Add two rows to internal tablecodes.setValue("I", "SIGN");codes.setValue("EQ", "OPTION");codes.setValue("P1001087", "MATNR_LOW");codes.setValue("", "MATNR_HIGH");
codes.nextRow(); // Move onto next rowcodes.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 tableSystem.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 tableSystem.out.println(
function.getTableParameterList().getValue("MATNRSELECTION"));System.out.println(
function.getTableParameterList().getString("MATNRSELECTION"));
} catch (Exception ex) {ex.printStackTrace();System.exit(1);
}
![Page 3: JCo code to call SAP function module](https://reader036.vdocument.in/reader036/viewer/2022082318/551cfd9649795964198b4792/html5/thumbnails/3.jpg)
}
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.");
}}
}
Using the Java connector (JCo) to retrieve data from an SAP system
/// This Program ll work if u have are your pre-requirements completed.. //Do post if you guys need more help on this.
import com.sap.mw.jco.*; public class CallFunction extends Object { JCO.Client mConnection; JCO.Repository mRepository; JCO.Table codes; public JCO.Function createFunction (String name) throws Exception { IFunctionTemplate ft = mRepository.getFunctionTemplate (name.toUpperCase ()); if (ft == null) return null; return ft.getFunction (); } public void Connect1() { try { // Change the logon information to your own system/user mConnection =
![Page 4: JCo code to call SAP function module](https://reader036.vdocument.in/reader036/viewer/2022082318/551cfd9649795964198b4792/html5/thumbnails/4.jpg)
JCO.createClient("800", // SAP client "******", // userid "******", // password "EN", // language "server", // application server host name "01"); // system number mConnection.connect(); //System.out.println(mConnection.getAttributes()); mRepository = new JCO.Repository ("SAPJCorep", mConnection); JCO.Function function = null; function = this.createFunction("BAPI_COMPANYCODE_GETLIST"); mConnection.execute(function); JCO.Structure returnStructure = function.getExportParameterList().getStructure ("RETURN"); if (! (returnStructure.getString("TYPE").equals("") || returnStructure.getString("TYPE").equals("S")) ) { System.out.println("Returned Message :" + returnStructure.getString ("MESSAGE")); System.exit (1); } System.out.println("Returned Name :" + returnStructure.getName().toString()); ////////////////////////////////////////////////////////////////////////////// JCO.Table codes = null; codes = function.getTableParameterList ().getTable("COMPANYCODE_LIST"); for (int i = 0; i < codes.getNumRows(); i++) { codes.setRow(i); System.out.println(codes.getString("COMP_CODE") + ' ' + codes.getString("COMP_NAME")); } // JCO.Table codes = null; codes = function.getTableParameterList ().getTable("COMPANYCODE_LIST"); for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow() ) { System.out.println(codes.getString("COMP_CODE") + ' ' + codes.getString("COMP_NAME") ); }
/////////////////////////////////////////////////////////////////////////// mConnection.disconnect(); } catch (Exception ex) { ex.printStackTrace(); System.exit(1); }
![Page 5: JCo code to call SAP function module](https://reader036.vdocument.in/reader036/viewer/2022082318/551cfd9649795964198b4792/html5/thumbnails/5.jpg)
}
public static void main (String args[]) { CallFunction app = new CallFunction(); app.Connect1(); } }