Sub topic 5
Partial Success with Database.insert and allOrNone
This chapter assumes you can write basic Apex, run DML statements such as insert and update, use try/catch with DmlException, and know that governor limits exist. It builds on the previous chapter, where a single DML statement is all-or-none.
The default: one bad record fails the statement
A DML statement such as insert accts; is all-or-none. If any record in the list fails, Apex throws a DmlException and none of the records in that statement are saved. This is the right default when the records only make sense together.
Sometimes you want a different outcome. In a data load, for example, you may prefer to save the valid rows and report the bad ones.
The allOrNone parameter
The Database class has methods that mirror the DML statements, such as Database.insert, Database.update, and Database.delete. They take a Boolean allOrNone parameter.
true(the default) behaves like the DML statement. One failure fails the whole call, and the call throws aDmlException.falseallows partial success. Records that pass validation are saved. Records that fail are not saved, and the call does not throw an exception for them.
With allOrNone set to false, the method returns an array of result objects, one per record, in the same order as the input list. For insert and update the type is Database.SaveResult.
List<Account> accts = new List<Account>{
new Account(Name = 'Good'),
new Account() // missing required Name
};
Database.SaveResult[] results = Database.insert(accts, false);
for (Database.SaveResult sr : results) {
if (!sr.isSuccess()) {
for (Database.Error err : sr.getErrors()) {
System.debug(err.getStatusCode() + ': ' + err.getMessage());
}
}
}The first account is saved. The second is not, and its SaveResult holds one or more Database.Error objects. Each error gives a status code, a message, and the fields involved.
Reading the results
Because no exception is thrown, your code must check the results. If you skip the check, failures are silent.
For each SaveResult:
isSuccess()returnstrueif the record was saved.getId()returns the new record Id when the save succeeded.getErrors()returns the list ofDatabase.Errorobjects when it failed.
The result at index i matches the record at index i in the input list. Use that to report which record failed.
Common errors
- Assuming no exception means success. With
allOrNoneset tofalse, a failed record does not throw. Always loop over the results. - Expecting no exception ever. The
falsesetting covers record-level failures such as validation errors and missing required fields. Some problems still throw, such as a null list passed to the method, or exceeding a governor limit. - Treating partial success as a transaction. Records that saved stay saved. If a later step fails and you need to undo them, you need a savepoint, which later chapters cover.
Exercise
You call Database.insert(accts, false) with 200 accounts, and 3 of them fail validation. How many accounts are saved, does the call throw a DmlException, and how do you find out which 3 failed?
Show answer
197 accounts are saved. The call does not throw a DmlException for the validation failures. You loop over the returned Database.SaveResult array and check isSuccess() on each one. The result at index i matches the account at index i in the input list, and getErrors() gives the reason for each failure.