Example Program to insert a row using BAPI with JCO3

Gana picture Gana · Oct 27, 2013 · Viewed 9k times · Source

I am trying to "insert" (or) "add a row" to Purchase Requisition using standard BAPI (PurchaseRequisition.CreateFromData).

I am using JCo3. The example in JCo3 indicates that we should use table.appendRow() OR table.insertRow() methods. I am trying with table.appendRow() & table.appendRows(1). When i try to insert a row, i dont get any error and the row is not inserted.

Below is the program i am trying to execute. /** Below are the inputs required for this program to run / / Step 1 **/ String 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));

//skipped Other columns related code

        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();
        }
    }

I did not understand how to read the response and am trying to fetch it from exportParameters!!

  1. Can anybody share a piece of code to insert and
  2. getting confirmation response (do we get the PREQ_NO in response?)
  3. I am adding date field value as "20131101", but not sure if the format and approach is right?
  4. when i try to add Quantity column value, i get an error message complaining this column is not part of BAPIEBANC. But the column is visible in BAPIEBANC type.
  5. any configuration on SAP side to be checked?
  6. should i activate any fields in JCo side? if so, how

Please note that my knowledge on SAP is very limited.

Waiting for an expert's response.

Thanks.

Answer

Carsten Erker picture Carsten Erker · Nov 4, 2013

First, you should take a look at SAP JCo documentation, e.g. http://help.sap.com/saphelp_nw04/helpdata/en/6f/1bd5c6a85b11d6b28500508b5d5211/content.htm

Regarding your code:

  • Adding (one) row to the table looks right on first sight.
  • Your code says QUANITY instead of QUANTITY.
  • You should add date values as java.util.Date; if creating a Date from a String format, you should use java.text.DateFormat.parse(). See http://docs.oracle.com/javase/6/docs/api/java/util/Date.html (this is however Java specific and has nothing to do with JCo).
  • If changing anything in SAP, never forget to call BAPI_TRANSACTION_COMMIT in the end to finish the logical unit of work (aka transaction) or nothing will actually be changed.

If you don't like to fiddle with the more or less complicated and verbose JCo API, try using Hibersap which gives you a much nicer programming model when calling functions in SAP ERP: http://hibersap.org.

However, you will still need a basic understanding on how SAP function modules work technically (such as parameter types or data types) as well as on the domain specific model which lies behind them (in your case, creating a requisition). I.e. you may need to communicate with your SAP experts.