cancel
Showing results for 
Search instead for 
Did you mean: 

Inserting data into SAP tables via java jco code

Former Member
0 Kudos

Hi Team,

          I need some help on inserting data into SAP tables via jco. I have analysed the options and got to know that it is possible via BAPI functions. Can you please provide some sample code on how to insert data into SAP tables via JCO.

     Do we have to create individual BAPI functions to update each table?? please advice

Thanks in Advance

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

What if there are multiple RFC's?? Plz help me on this.

MarkusTolksdorf
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Seetal,

simply see the answer from Stefan above. He described already what to do in case of having multiple RFCs by telling that the solution for it is not needed when only invoking a single one.

Best regards,

Markus

Former Member
0 Kudos

HI All,

    For the benefit of others. From the analysis done so far, I was able to create new records via JCO code using the BAPI function 'BAPI_CUSTOMER_CREATEFROMDATA1' . Below is the sample code which worked for me. I was able to create new records using the BAPI function BAPI_CUSTOMER_CREATEFROMDATA1 via JCO.

public static void insertCustomerData() throws JCoException

    {

    JCoDestination destination  = JCoDestinationManager.getDestination(ABAP_AS_POOLED);

  JCoRepository repository = destination.getRepository();

  JCoContext.begin(destination);

  JCoFunction function = repository.getFunction("BAPI_CUSTOMER_CREATEFROMDATA1");

  if(function == null)

            throw new RuntimeException("BAPI_CUSTOMER_CREATEFROMDATA1" + " not found in SAP.");

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

  JCoStructure codes = function.getImportParameterList().getStructure("PI_PERSONALDATA");

  codes.setValue("LASTNAME", "John Ray");

  codes.setValue("FIRSTNAME", "John Ray");

  codes.setValue("CITY", "Texsas Vd");

  codes.setValue("LANGU_P", "EN");

  codes.setValue("LANGUP_ISO", "EN");

  codes.setValue("COUNTRY", "US");

  codes.setValue("CURRENCY", "DOLLARS");

  codes.setValue("POSTL_COD1", "103258");

  codes = function.getImportParameterList().getStructure("PI_COPYREFERENCE");

  codes.setValue("REF_CUSTMR", "000014523");

  codes.setValue("SALESORG", "1250");

  codes.setValue("DISTR_CHAN", "20");

  codes.setValue("DIVISION", "20");

  function.execute(destination);

  

  String cusnumber = function.getExportParameterList().getString("CUSTOMERNO") ;

  

  System.out.println("The generated customer number is" + cusnumber);

  

  

    }

HAL9000
Product and Topic Expert
Product and Topic Expert
0 Kudos

Without having a call sequence of several RFC calls [multiple function.execute(destination) calls] you do not need a stateful call context and should not use JCoContext.begin(destination).

But if you do, you should also always end the stateful call sequence again - also in error or exception situations - with calling JCoContext.end(destination) ideally in a try-finally-block. Otherwise you created a connection leak and you are possibly destroying the integrity of the current LUW with still having uncommitted operations in your current RFC context.

Please see the JCo API documentation at class JCoContext for further infos.

Furthermore I recommend to check if BAPI_TRANSACTION_COMMIT really doesn't need to be called afterwards for the new customer data to get persisted in the database. Usually BAPIs are not doing an ABAP 'commit work' automatically - at least they should not. In this case, the stateful call context would indeed be needed for the two calls BAPI_CUSTOMER_CREATEFROMDATA1 and BAPI_TRANSACTION_COMMIT.

Best regards,

Stefan