Skip to Content
avatar image
Former Member

SAP JCo Get notice of unrestricted use or error after sending IDOC

Hi everyone,

This is my first question here. I'm developing a WMS wich is interfacing with SAP via IDocs using Java Connector 3. Everything is woking well, except that I dont know how to perform a rollback in Java side, when SAP doesn't process the IDoc for any reason, example unrestricted use of material when moving stock or batch.

So the question is, how can I get notice of these kind of errors after sending the IDoc?

Here is the code I'm using:

public void process() {

try{

doSomeStuff();

sendIDoc();

commit();

} catch (Exception ex){

rollback();

}

public void sendIDoc() {

try {

JCoDestination destination = JCoDestinationManager.getDestination(serverName);
IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
String tid = destination.createTID();
IDocFactory iDocFactory = JCoIDoc.getIDocFactory();

IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
IDocDocumentList iDocList = processor.parse(iDocRepository, xml);
JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
destination.confirmTID(tid);

} catch (Exception ex) {
ex.printStackTrace();
throw new Exception(ex);
}

}

Thanks in advance.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Nov 27, 2017 at 07:22 AM

    Hello Federico, According to your description I think this should a JCO inbound call Java invoke ABAP (RFC Call). Usually, when calling RFC we will define a call input and output parameter. the output type depends on the RFC function return type. If you want to get some messages from ABAP you have to return the message from ABAB side and then catch those messages from JAVA side by using output parameter.

    The code snippet example shows you how to define and use an output.

    // Create the output parameter list from the metadata object
    JCO.ParameterList output = JCO.createParameterList(output_md);
    // Call the function
    client.execute("STFC_CONNECTION", input, output);
    
    Add comment
    10|10000 characters needed characters exceeded

    • The sample code

      import com.sap.mw.jco.*;
      
      
      public class Example1 {
      
      
        public static void main(String[] argv)
        {
          JCO.Client client = null;
      
      
          try {
      
      
            // Print the version of the underlying JCO library
            System.out.println("\n\nVersion of the JCO-library:\n" +
                                   "---------------------------\n" + JCO.getMiddlewareVersion());
      
      
            // Create a client connection to a dedicated R/3 system
            client = JCO.createClient( "000",       // SAP client
                      				 "johndoe",   // userid
                      				 "*****",     // password
                      				 "EN",        // language
                      				 "appserver", // host name
                      				 "00" );      // system number
      
      
            // Open the connection
            client.connect();
      
      
            // Get the attributes of the connection and print them
      
      
            JCO.Attributes attributes = client.getAttributes();
            System.out.println("Connection attributes:\n" +
                               "----------------------\n" + attributes);
            boolean is_backend_unicode = attributes.getPartnerCodepage().equals("4102") ||
                                         attributes.getPartnerCodepage().equals("4103");
      
      
            // Create metadata definition of the input parameter list
            JCO.MetaData input_md = new JCO.MetaData("INPUT");
            input_md.addInfo("REQUTEXT", JCO.TYPE_CHAR, 255, 255 * (is_backend_unicode? 2 : 1 ),
                              -1, 0, null, null, 0, null, null);
      
      
            // Create the input parameter list from the metadata object
            JCO.ParameterList input = JCO.createParameterList(input_md);
      
      
            // Set the first (and only) input parameter
            input.setValue("This is my first JCo example.", "REQUTEXT");
      
      
            // Create metadata definition of the output parameter list
            JCO.MetaData output_md = new JCO.MetaData("OUTPUT");
      
      
            // Specify the parameters types of the function will be returned
            output_md.addInfo("ECHOTEXT", JCO.TYPE_CHAR, 255, 255 * (is_backend_unicode? 2 : 1 ),
                               -1, 0, null, null, 0, null, null);
            output_md.addInfo("RESPTEXT", JCO.TYPE_CHAR, 255, 255 * (is_backend_unicode? 2 : 1 ),
                               -1, 0, null, null, 0, null, null);
      
      
            // Create the output parameter list from the metadata object
            JCO.ParameterList output = JCO.createParameterList(output_md);
      
      
            // Call the function
            client.execute("STFC_CONNECTION", input, output);
      
      
            // Print the result
            System.out.println("The function 'STFC_CONNECTION' returned the following parameters:\n" +
                               "-----------------------------------------------------------------");
            for (int i = 0; i < output.getFieldCount(); i++) {
                System.out.println("Name: " +  output.getName(i) + " Value: " + output.getString(i));
            }//for
      
      
            // All done
            System.out.println("\n\nCongratulations! It worked.");
          }
          catch (Exception ex) {
            System.out.println("Caught an exception: \n" + ex);
          }
          finally {
              // do not forget to close the client connection
              if (client != null) client.disconnect();
          }
        }
      }