Search code examples
sap-erpbapijco

Unable to create Sales Order from JCO


I am using the below code to call BAPI_SALESORDER_CREATEFROMDAT2 to create the Sales Order. SAP ERP software is generating a SO number and sending as a response but when I check with va03 for the SO number which I have received, sales order is not created .

If I do manually using va01 with the same data I can create the sales order successfully. Please need help on this .

public static void createSalesOrder() {
    try {
        JCoDestination destination = JCoDestinationManager.getDestination("ABAP_AS_WITH_POOL");
        JCoFunction functionCreateOrder = destination.getRepository().getFunction("BAPI_SALESORDER_CREATEFROMDAT2");
        //this is the bapi 
        JCoFunction functionTransComit = destination.getRepository().getFunction("BAPI_TRANSACTION_COMMIT");

        JCoStructure orderHeaderIn = functionCreateOrder.getImportParameterList().getStructure("ORDER_HEADER_IN");
        orderHeaderIn.setValue("SALES_ORG", "2000");//sales organisation
        orderHeaderIn.setValue("DISTR_CHAN", "20");//distribution channel
        orderHeaderIn.setValue("DIVISION", "20");// sales division 
        orderHeaderIn.setValue("DOC_TYPE", "ZAR");// document type 
        orderHeaderIn.setValue("PURCH_NO_C", "TEST123");
        

        JCoTable orderPartners = functionCreateOrder.getTableParameterList().getTable("ORDER_PARTNERS");
        // WE,AG,SP,PH
        // AG Sold to Party
        // WE Ship to Partyx
        orderPartners.appendRow();
        orderPartners.setValue("PARTN_ROLE", "AG");//partner role ag is sold to party 
        orderPartners.setValue("PARTN_NUMB", "0000000035");// partner number 
        orderPartners.appendRow();
        orderPartners.setValue("PARTN_ROLE", "WE");//we is ship tp party 
        orderPartners.setValue("PARTN_NUMB", "0000000035");// partner number 
        System.out.println(orderPartners);

        JCoTable orderItemsIn = functionCreateOrder.getTableParameterList().getTable("ORDER_ITEMS_IN");
        orderItemsIn.appendRow();
        orderItemsIn.setValue("MATERIAL", "PEN_ARN");// material 
        System.out.println(orderItemsIn);

        JCoTable orderSchedulesIn = functionCreateOrder.getTableParameterList().getTable("ORDER_SCHEDULES_IN");
        orderSchedulesIn.appendRow();
        orderSchedulesIn.setValue("REQ_QTY", "10");// required quantity 
        System.out.println(orderSchedulesIn);

        functionCreateOrder.execute(destination);
        

        // System.out.println(functionCreateOrder);
        JCoTable returnTable = functionCreateOrder.getTableParameterList().getTable("RETURN");
        System.out.println(returnTable);
        System.out.println(returnTable.getString("MESSAGE"));
        System.out.println("sales order number is : "
                + functionCreateOrder.getExportParameterList().getValue("SALESDOCUMENT"));
        functionTransComit.execute(destination);

    } catch (JCoException ex) {
        System.out.println(ex.getMessage());
    } finally {
        System.out.println("Creating sales order ends");
    }

}

Solution

  • This depends on your JCo version, but at least with JCo 3 you need to execute your call to BAPI_TRANSACTION_COMMIT within the same context as your function call to BAPI_SALESORDER_CREATEFROMDAT2. Currently both calls are executed in individual contexts, so the second call to BAPI_TRANSACTION_COMMIT doesn't really commit anything. You have to create a context first:

    JCoContext.begin(destination);
    
    // execute both function calls to 
    // BAPI_SALESORDER_CREATEFROMDAT2 and
    // BAPI_TRANSACTION_COMMIT
    
    JCoContext.end(destination);