Creating purchase orders using SAP Java Connector (JCo)
This application uses classes in JCo to call a BAPI to create purchase order in the SAP system.
Proper use of this tip expects the user to at least understand Core Java. The SAP Java Connector is a midleware...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
that allows us to communicate with SAP system using JAVA applications. The example included is of client-side programming, i.e. Java to SAP call. Here a Java application has been developed using Frame (a class in Java); this application uses classes in JCo to call a BAPI to create purchase order in the SAP system. I have not included many validations in the application, but neverthless, I guess this source code will prove to be an ice-breaker for you guys as far as using JCo is concerned. Note: Obviously, you need to have JCo class files in your class path!
import com.sap.mw.jco.*; import java.util.*; import java.applet.*; import java.awt.*; import java.awt.event.*; public class BAPITip extends Frame implements ActionListener { //Global Declarations int i = 0; char flag = 'a'; Label doc_type,co_code,purch_org,pur_group,vendor; Label po_item,pur_mat,plant,unit,net_price,deliv_date,quantity; TextField DOC_TYPE,CO_CODE,PURCH_ORG,PUR_GROUP,VENDOR; TextField PO_ITEM[],PUR_MAT[],PLANT[],UNIT[],NET_PRICE[],DELIV_DATE[],QUANTITY[]; Panel pan_hdr,pan_itm,pan_bs,pan_st; Button bt = null,bc = null,br = null,bd = null; TextArea status = null; JCO.Client mConnection = null; JCO.Pool pool = null; static final String POOL_NAME = "MyPool"; static JCO.Repository mRepository; String msg = ""; public BAPITip() { setLocation(0,0); setSize(800,800); setVisible(true); bt = new Button("Submit"); bc = new Button("Connect"); br = new Button("Reset"); bd = new Button("Disconnect"); bt.addActionListener(this); bc.addActionListener(this); br.addActionListener(this); bd.addActionListener(this); status = new TextArea("Status",4,30); status.setEditable(false); //Instantiating header labels doc_type = new Label("Document Type"); co_code = new Label("Company Code"); purch_org = new Label("Purchasing Organization"); pur_group = new Label("Purchasing Group"); vendor = new Label("Vendor"); //Instantiating line item labels po_item = new Label(" Item number"); pur_mat = new Label(" Material"); plant = new Label(" Plant"); unit = new Label(" Unit"); net_price = new Label(" Net price"); deliv_date = new Label(" Delivery date"); quantity = new Label(" Quantity"); //Instantiating header text fields DOC_TYPE = new TextField(); CO_CODE = new TextField(); PURCH_ORG = new TextField(); PUR_GROUP = new TextField(); VENDOR = new TextField(); //Instantiating line item textfields PO_ITEM = new TextField[5]; PUR_MAT = new TextField[5]; PLANT = new TextField[5]; UNIT = new TextField[5]; NET_PRICE = new TextField[5]; DELIV_DATE = new TextField[5]; QUANTITY = new TextField[5]; //Instantiating panels pan_hdr = new Panel(); pan_itm = new Panel(); //Setting layout for applet and adding component-panels to applet setLayout(new GridLayout(4,1)); //Setting layout for header panel and adding components GridLayout gl1 = new GridLayout(5,2); pan_hdr.setLayout(gl1); pan_hdr.add(doc_type); pan_hdr.add(DOC_TYPE); pan_hdr.add(co_code); pan_hdr.add(CO_CODE); pan_hdr.add(purch_org); pan_hdr.add(PURCH_ORG); pan_hdr.add(pur_group); pan_hdr.add(PUR_GROUP); pan_hdr.add(vendor); pan_hdr.add(VENDOR); add(pan_hdr); //Setting layout for line item panel and adding components GridLayout gl2 = new GridLayout(7,7); pan_itm.setLayout(gl2); pan_itm.add(po_item); pan_itm.add(pur_mat); pan_itm.add(plant); pan_itm.add(unit); pan_itm.add(net_price); pan_itm.add(deliv_date); pan_itm.add(quantity); for(i = 0; i < 5; i++) { PO_ITEM[i] = new TextField(); PUR_MAT[i] = new TextField(); PLANT[i] = new TextField(); UNIT[i] = new TextField(); NET_PRICE[i] = new TextField(); DELIV_DATE[i] = new TextField(); QUANTITY[i] = new TextField(); pan_itm.add(PO_ITEM[i]); pan_itm.add(PUR_MAT[i]); pan_itm.add(PLANT[i]); pan_itm.add(UNIT[i]); pan_itm.add(NET_PRICE[i]); pan_itm.add(DELIV_DATE[i]); pan_itm.add(QUANTITY[i]); } pan_itm.add(bc); pan_itm.add(bt); pan_itm.add(br); pan_itm.add(bd); add(pan_itm); add(status); } public void actionPerformed(ActionEvent ae) { msg = ""; if (ae.getSource() == bt) this.submitToSAP(); else if (ae.getSource() == bc) this.connectDirectToSAP(); else if (ae.getSource() == br) this.resetValues(); else if (ae.getSource() == bd) this.disconnectFromSAP(); } private void connectDirectToSAP() { // Change the logon information to your own system/user mConnection = JCO.createClient("210", // SAP client "DEV07", // userid "dra", // password "EN", // language (null for the default language) "10.111.16.26", // application server host name "00"); // system number try { mConnection.connect(); msg = "Connection successfully established"; this.setStatus(); System.out.println("Connection successfully established"); } catch (Exception ex) { msg = ex.getMessage(); this.setStatus(); System.exit(1); } } private void resetValues() { DOC_TYPE.setText(""); PURCH_ORG.setText(""); PUR_GROUP.setText(""); CO_CODE.setText(""); VENDOR.setText(""); status.setText(""); for (int i = 0; i < 5; i++) { PO_ITEM[i].setText(""); PUR_MAT[i].setText(""); PLANT[i].setText(""); UNIT[i].setText(""); NET_PRICE[i].setText(""); DELIV_DATE[i].setText(""); QUANTITY[i].setText(""); } } private void submitToSAP() { msg = "Please wait..."; this.setStatus(); System.out.println("Please wait..."); //creation of Repository object and object for the BAPI if(mConnection == null) { msg = "Please conect first to the SAP system"; this.setStatus(); System.out.println(msg); //System.exit(0); } mRepository = new JCO.Repository("ARAsoft", mConnection); JCO.Function function = null; function = BAPITip.createFunction("BAPI_PO_CREATE"); //setting the import parameters JCO.Structure PO_HDR = function.getImportParameterList() .getStructure("PO_HEADER"); JCO.Table PO_LITEMS = function.getTableParameterList().getTable("PO_ITEMS"); JCO.Table PO_ITM_SCH = function.getTableParameterList().getTable("PO_ITEM_SCHEDULES"); PO_HDR.setValue(DOC_TYPE.getText(),"DOC_TYPE"); PO_HDR.setValue(CO_CODE.getText(),"CO_CODE"); PO_HDR.setValue(PURCH_ORG.getText(),"PURCH_ORG"); PO_HDR.setValue(PUR_GROUP.getText(),"PUR_GROUP"); PO_HDR.setValue(VENDOR.getText(),"VENDOR"); for(int i = 0; i < 5; i++) { ; if (PO_ITEM[i].getText() != "") { PO_LITEMS.appendRow(); PO_LITEMS.setValue(PO_ITEM[i].getText(),"PO_ITEM"); PO_LITEMS.setValue(PUR_MAT[i].getText(),"PUR_MAT"); PO_LITEMS.setValue(PLANT[i].getText(),"PLANT"); PO_LITEMS.setValue(UNIT[i].getText(),"UNIT"); PO_LITEMS.setValue(NET_PRICE[i].getText(),"NET_PRICE"); PO_ITM_SCH.appendRow(); PO_ITM_SCH.setValue(PO_ITEM[i].getText(),"PO_ITEM"); PO_ITM_SCH.setValue(new Date(),"DELIV_DATE"); PO_ITM_SCH.setValue(QUANTITY[i].getText(),"QUANTITY"); } } //executing the BAPI mConnection.execute(function); //accessing export parameters JCO.Table returnTable = function.getTableParameterList().getTable("RETURN"); if(returnTable.getNumRows() != 0) { for (int i = 0; i < returnTable.getNumRows(); i++) { returnTable.setRow(i); if (! (returnTable.getString("TYPE").equals("") || returnTable.getString("TYPE").equals("S") || returnTable.getString("TYPE").equals("W")) ) { if (i == 0) status.setText(""); msg = returnTable.getString("MESSAGE"); System.out.println(returnTable.getString("MESSAGE")); } } } String PO_NUM = function.getExportParameterList(). getString("PURCHASEORDER"); if (PO_NUM != null) { flag = 'a'; msg = "Purchase order " + PO_NUM + " created successfully!"; this.setStatus(); System.out.println(msg); } } private void disconnectFromSAP() { mConnection.disconnect(); msg = "Disconnected from SAP system..."; this.setStatus(); System.out.println("Disconnected from SAP system..."); } public static JCO.Function createFunction(String name) { IFunctionTemplate ft = mRepository.getFunctionTemplate(name.toUpperCase()); if (ft == null) return null; return ft.getFunction(); } private void setStatus() { status.setText(""); status.setText(msg); } private void appendStatus() { status.append(" " + msg); } public static void main(String args[]) { BAPITip fb = new BAPITip(); }