2013-10-27 16 views
0

我試圖使用標準BAPI(PurchaseRequisition.CreateFromData)向採購申請「插入」(或)「添加一行」。示例使用帶JCO3的BAPI插入行的程序

我正在使用JCo3。 JCo3中的例子表明我們應該使用table.appendRow()或table.insertRow()方法。我正在嘗試table.appendRow()& table.appendRows(1)。當我嘗試插入一行時,我沒有得到任何錯誤,行未插入。

下面是我試圖執行的程序。 /**以下是該程序運行所需的輸入/ /步驟1 **/ 字符串BAPI_NAME =「BAPI_REQUISITION_CREATE」;

/** Step 2 **/ 
    String query_input_column1 = "DOCUMENTY_TYPE"; 
    String query_input_column1_value = "NB"; 

    String query_input_column2 = "PREQ_NAME"; 
    String query_input_column2_value = "Name"; 

    String query_input_column3 = "ACCTASSCAT"; 
    String query_input_column3_value = "U"; 

    String query_input_column4 = "DELIV_DATE"; 
    String query_input_column4_value = "20131101"; 

    String query_input_column5 = "MATERIAL"; 
    String query_input_column5_value = "DELL-RQ2013"; 

    String query_input_column6 = "QUANITY"; 
    int query_input_column6_value = 10100; 




    /** Step 3 **/ 
    String targetTableUnderBAPI = "REQUISITION_ITEMS"; 

    /** Step 4 **/ 
    /** For the confirmation read the value from export parameter after insertion execution **/ 
    String result_column1 = "NUMBER"; 


    JCoDestination destination = null; 
    try { 
     destination = JCoDestinationManager.getDestination(DestinationManager.DESTINATION_NAME1); 
     JCoRepository repository = destination.getRepository(); 
     JCoContext.begin(destination); 

     JCoFunction function = repository.getFunction(BAPI_NAME); 

     if(function == null) 
      throw new RuntimeException(BAPI_NAME + " not found in SAP."); 

     System.out.println("BAPI Name from function object: " + function.getName());    

     //function.getImportParameterList().setValue(query_input_column1, query_input_column1_value); 
     JCoTable table = function.getTableParameterList().getTable(targetTableUnderBAPI); //it is taken from the response value of metadata 
     //System.out.println("No of Columns: "+ table.getNumColumns()); 
     System.out.println("Trying to execute append row"); 

     table.appendRow(); 
      table.setValue(query_input_column1,query_input_column1_value); 
      table.setValue(query_input_column2,query_input_column2_value); 
      table.setValue(query_input_column3,query_input_column3_value); 
      //table.setValue(query_input_column4,new java.util.Date(query_input_column4_value)); 

//跳過其他列相關的代碼

 try{ 
      function.execute(destination); 
     } 
     catch(AbapException e){ 
      System.out.println(e.toString()); 
      return; 
     } 

     System.out.println("Let us check the result from export parameter"); 
     String exportParamStructure = (String)function.getExportParameterList().getValue(result_column1); //getStructure(result_column1); // getValue(result_column1); 
     System.out.println("Resulting PR#: "+exportParamStructure); 

    } catch (JCoException e) { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     try { 
      JCoContext.end(destination); 
     } catch (JCoException e) { 
      e.printStackTrace(); 
     } 
    } 

我不知道如何讀的響應,並且想從exportParameters獲取它!

  1. 任何人都可以分享一段代碼插入和
  2. 獲得確認的響應(我們響應得到PREQ_NO?)
  3. 我加入日期字段值爲「20131101」,但不能肯定是否格式和方法是正確的?
  4. 當我嘗試添加數量列值時,我收到一條錯誤消息,抱怨此列不是BAPIEBANC的一部分。但該列在BAPIEBANC類型中可見。
  5. 要檢查SAP端的任何配置?
  6. 我應該激活JCo方面的任何領域嗎?如果是這樣,

請注意,我在SAP方面的知識非常有限。

等待專家的迴應。

謝謝。

+0

你好,如何在jco表中添加多個原始數據?使用RFC –

回答

1

首先,您應該看看SAP JCo文檔,例如 http://help.sap.com/saphelp_nw04/helpdata/en/6f/1bd5c6a85b11d6b28500508b5d5211/content.htm

關於你的代碼:

  • 添加(一個)排表看起來正確的一見鍾情。
  • 您的代碼說QUANITY而不是QUANTITY。
  • 您應該將日期值添加爲java.util.Date;如果從String格式創建Date,則應該使用java.text.DateFormat.parse()。請參閱http://docs.oracle.com/javase/6/docs/api/java/util/Date.html(但這是Java特有的,與JCo無關)。
  • 如果更改SAP中的任何內容,最後永遠不要忘記調用BAPI_TRANSACTION_COMMIT來完成邏輯工作單元(又名事務),或者實際上不會更改任何內容。

如果你不喜歡用或多或少複雜和冗長JCO API亂動,請嘗試使用Hibersap調用SAP ERP的功能,當它給你一個更加美好的編程模型:http://hibersap.org

但是,您仍然需要了解SAP功能模塊如何在技術上工作(如參數類型或數據類型)以及背後的特定於域的模型(在您的情況下,創建請購單) 。即您可能需要與您的SAP專家溝通。

1

在這裏,我加入2種類型的插入的:

  1. insertval()函數爲用戶定義的模塊駐留在樹液與ABAP程序員
  2. 的幫助它的一個標準模塊,用於插入件使用JCO到SOLMAN系統票證。首先,您必須分析導入,導出,表&結構參數,並根據該值必須傳遞值並檢索響應。在第二個功能中,它將在solman成功插入票證後返回票證n°。

我希望這個示例代碼將幫助你,它爲我工作。

public class jco 
    { 

    static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL"; 
     static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL"; 

    static 
     { 

      Properties connectProperties = new Properties(); 
      connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.1.1.1"); 
      connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "01"); 
      connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "500"); 
      connectProperties.setProperty(DestinationDataProvider.JCO_USER, "uname"); 
      connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "pwd"); 
      connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en"); 

      createDestinationDataFile(DESTINATION_NAME1, connectProperties); 
      connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); 
      connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); 
      createDestinationDataFile(DESTINATION_NAME2, connectProperties); 
      System.err.println("hai"); 

     } 

     static void createDestinationDataFile(String destinationName, Properties connectProperties) 
     { 
      File destCfg = new File(destinationName+".jcoDestination"); 
      try 
      { 
       try (FileOutputStream fos = new FileOutputStream(destCfg, false)) { 
        connectProperties.store(fos, "for tests only !"); 
       } 
      } 
      catch (IOException e) 
      { 
       throw new RuntimeException("Unable to create the destination files", e); 
      } 
     } 






     public void insertval() throws JCoException 
     { 

      JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1); 

      JCoFunction jf=destination.getRepository().getFunction("ZUSER_DET"); 

     jf.getImportParameterList().setValue("FIRST_NAME","member"); 
     jf.getImportParameterList().setValue("LAST_NAME","c"); 
     jf.getImportParameterList().setValue("USER_NO","1000"); 

     jf.execute(destination); 
    System.out.println(jf); 
     } 





public void insertticket() throws JCoException 
    { 
     JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2); 
     System.out.println("test"+"\n"); 
     JCoFunction jf=destination.getRepository().getFunction("BAPI_NOTIFICATION_CREATE"); 

     JCoTable jt1=jf.getTableParameterList().getTable("APPX_HEADERS"); 


     JCoTable jt2=jf.getTableParameterList().getTable("APPX_LINES"); 


     JCoTable jt3=jf.getTableParameterList().getTable("APPX_LINES_BIN"); 


     JCoTable jt4=jf.getTableParameterList().getTable("NOTIF_NOTES"); 



     JCoTable jt5=jf.getTableParameterList().getTable("NOTIF_PARTNERS"); 
     JCoTable jt6=jf.getTableParameterList().getTable("NOTIF_SAP_DATA"); 
     JCoTable jt7=jf.getTableParameterList().getTable("NOTIF_TEXT_HEADERS"); 
     JCoTable jt8=jf.getTableParameterList().getTable("NOTIF_TEXT_LINES"); 


     JCoStructure jfn1=jf.getImportParameterList().getStructure("NOTIF_EXT"); 
    JCoStructure jfn2=jf.getImportParameterList().getStructure("NOTIF_CRM"); 
JCoStructure jfn3=jf.getImportParameterList().getStructure("IBASE_DATA"); 
jfn1.setValue("NUMB","1234"); 
jfn1.setValue("REFNUM","123"); 
jfn1.setValue("TYPE_NOTIF","SLFN"); 
jfn1.setValue("SUBJECT","tl"); 
jfn1.setValue("PRIORITY","2"); 
jfn1.setValue("LANGUAGE","EN"); 
jfn1.setValue("CATEGORY","Z01"); 
jfn2.setValue("CODE","0011"); 
jfn2.setValue("CODEGROUP","0011"); 
    jfn2.setValue("CATEGORY","Z01"); 
jfn3.setValue("INSTANCE","489"); 
    jfn3.setValue("IBASE","500"); 



jt1.appendRow(); 
jt1.setValue("DESCR","practise"); 
jt2.appendRow(); 

     jt2.setValue("LINE","CVXCVXCV"); 
    jt3.appendRow(); 
     jt3.setValue("LINE","second text line"); 
     jt4.appendRow(); 
     jt4.setValue("TYPE_NOTE","my"); 
     jt4.setValue("IDENT","hoe twwrtgw"); 
     jt4.setValue("DESCRIPTION","its description "); 
jt5.appendRow(); 

jt5.setValue("PARNR","new "); 
jt5.setValue("TYPE_PAR","FN"); 
jt5.setValue("FUNC_PAR","EN"); 
jt5.setValue("PAR_ACTIVE","1"); 
jt6.appendRow(); 
jt6.setValue("INSTN","0020214076"); 
jt6.setValue("COMP","FI-AA"); 
jt6.setValue("SYSTYPE","P"); 
jt6.setValue("SYSID","PRD"); 
jt6.setValue("MANDT","900"); 
jt8.appendRow(); 
jt8.setValue("TXT_NUM","1"); 
jt8.setValue("TDFORMAT",">X"); 
jt8.setValue("TDLINE","/(performing all test)"); 






jf.execute(destination); 
String jfex=jf.getExportParameterList().getString("REFNUM"); 

       System.out.println("hi "+jfex); 

    } 
+0

感謝Ganesh的迴應。在過去的兩個月裏(問題發佈後),我的工作進展順利,我很樂意在JCo3中使用BAPI和RFM。無論如何,謝謝你的例子。這幾乎是一個完整的例子。 – Gana