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.
| 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. |
public void allOrNoneHeaderSample() { try { // create two contacts SObject[] sObjects = new SObject[2]; Contact contact1 = new Contact(); contact1.setFirstName("Robin"); contact1.setLastName("Van Persie"); // This contact doesn't have a value for the required // LastName field so the create won't work 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(); } } }