AllOrNoneHeader

The AllOrNoneHeader header allows a call to roll back all changes unless all records are processed successfully. The default behavior is to allow partial success of a call: records without errors are committed, while records with errors are marked as failed in the call results. This header is available in API version 20.0 and later.

Even if the header is enabled, it's still necessary to inspect the success field in the call result for each record to identify records with errors. All success fields are marked as true or false indicating whether the call was processed successfully. If there is an error associated with at least one record, the errors field in the call result for the record gives more information on the error. If other records in the same call have no errors, their errors fields indicate that they were rolled back due to failures in other records in the same call.

API Calls

create(), delete(), undelete(), update(), upsert()

Fields

Element Name Type Description
allOrNone boolean If true, any failed records in a call cause all changes for the call to be rolled back. Record changes aren't committed unless all records are processed successfully.

The default is false. Some records can be processed successfully while others are marked as failed in the call results.

Sample Code—Java

This sample shows how to use the AllOrNoneHeader. It attempts to create two contacts. The second contact doesn’t have all required fields set and causes a failure on creation. Next, the sample sets the AllOrNoneHeader to true, and then makes the call to create the contacts. Creating one of the contacts results in an error, so the entire transaction is rolled back and no contacts are created because the AllOrNoneHeader was set to true.
  public void allOrNoneHeaderSample() {
    try {
      // Create the first contact.
      SObject[] sObjects = new SObject[2];
      Contact contact1 = new Contact();
      contact1.setFirstName("Robin");
      contact1.setLastName("Van Persie");
  
      // Create the second contact. This contact doesn't 
      // have a value for the required
      // LastName field so the create will fail.
      Contact contact2 = new Contact();
      contact2.setFirstName("Ashley");
      sObjects[0] = contact1;
      sObjects[1] = contact2;
      
      // Set the SOAP header to roll back the create unless
      // all contacts are successfully created.
      connection.setAllOrNoneHeader(true);
      // Attempt to create the two contacts.
      SaveResult[] sr = connection.create(sObjects);
      for (int i = 0; i < sr.length; i++) { 
        if (sr[i].isSuccess()) {
          System.out.println("Successfully created contact with id: " + 
            sr[i].getId() + ".");
        }
        else {
          // Note the error messages as the operation was rolled back 
          // due to the all or none header.
          System.out.println("Error creating contact: " + 
            sr[i].getErrors()[0].getMessage());
          System.out.println("Error status code: " + 
            sr[i].getErrors()[0].getStatusCode());
        }
      }
    } catch (ConnectionException ce) {
      ce.printStackTrace();
    }
  }
}
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.