Sub topic 5
allOrNone: all-or-nothing versus partial success
This chapter assumes you can write basic Apex, use try/catch, and know that Apex runs in transactions with governor limits. You should also know the difference between a DML statement such as insert and a Database method such as Database.insert.
The two behaviors
When you save a list of records, one bad record raises a question: should the good records be saved anyway?
- All-or-nothing: if any record fails, none are saved. The call throws a
DmlException. - Partial success: valid records are saved and invalid ones are skipped. The call reports each failure and does not throw for record-level errors.
DML statements always use all-or-nothing. Database methods let you choose through the allOrNone parameter. Its default is true, so Database.insert(accounts) behaves like the insert statement. Passing false turns on partial success.
All-or-nothing with a statement
In this example, the second account has no Name, which is required.
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme'),
new Account() // Name is required, so this record fails
};
try {
insert accounts;
} catch (DmlException e) {
System.debug('Failed rows: ' + e.getNumDml());
System.debug(e.getMessage());
}The statement throws DmlException and rolls back the whole operation. Neither account is saved, including the valid one named Acme.
Partial success with allOrNone set to false
Now the same data goes through Database.insert with allOrNone set to false.
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme'),
new Account() // Name is required, so this record fails
};
List<Database.SaveResult> results = Database.insert(accounts, false);
for (Integer i = 0; i < results.size(); i++) {
Database.SaveResult sr = results[i];
if (sr.isSuccess()) {
System.debug('Inserted ' + sr.getId());
} else {
for (Database.Error err : sr.getErrors()) {
System.debug('Record ' + i + ' failed: ' + err.getStatusCode() + ' - ' + err.getMessage() + ' ' + err.getFields());
}
}
}Acme is saved. The call returns one SaveResult per input record, in the same order as the input list. No exception is thrown for the failed record. Because the order matches, index i in the results points to accounts[i].
Common errors
- Assuming
falsemeans no exceptions at all. Only record-level errors are returned in results. Problems such as exceeding a governor limit still throw exceptions, and you cannot catch a limit exception to continue normally. - Ignoring the results. With
allOrNoneset tofalse, a failure is silent unless you checkisSuccess(). If you discard the return value, bad records vanish without any sign. - Matching results to the wrong record. Use the index to pair each result with its input record. Do not rely on a result's ID, because failed records have none.
- Expecting partial rollback inside a statement. A
DmlExceptionfrom a statement means the whole operation was undone, not only the failing rows.
Choosing a setting
| Situation | Setting |
|---|---|
| Records depend on each other and a partial save would leave data inconsistent | All-or-nothing (statement or `allOrNone` true) |
| Importing many independent records where one bad row should not block the rest | Partial success (`allOrNone` false) |
| You need to report which rows failed and why | Partial success, then inspect the results |
Exercise
You call Database.update(contacts, false) with 50 contacts and 3 of them fail validation. How many records are updated, does the call throw a DmlException, and how do you find the failed contacts?
Show answer
47 records are updated. The call does not throw a DmlException for record-level validation errors. It returns a List<Database.SaveResult> with 50 entries in the same order as the input. Loop through it, check isSuccess() on each entry, and use the index to find the matching contact and getErrors() to see why it failed.