Sub topic 4
All-or-none behavior of a single DML statement
This chapter assumes you can write basic Apex, run insert and update on sObjects, use try/catch with DmlException, and know that governor limits exist. It covers what happens when one DML statement works on several records and one of them fails.
The default rule: one failure fails the statement
A DML statement such as insert accts; is atomic. Either every record in the list is saved, or none of them are. This is called all-or-none behavior. If one record fails validation, Apex throws a DmlException, and the records in that statement that would have succeeded are rolled back too.
List<Account> accts = new List<Account>{
new Account(Name = 'Good'),
new Account() // missing required Name
};
try {
insert accts;
} catch (DmlException e) {
System.debug('Failed: ' + e.getMessage());
}
System.debug([SELECT COUNT() FROM Account WHERE Name = 'Good']); // 0The Good account is not saved, even though it is valid. The statement treats the list as one unit of work.
Scope: the statement, not the transaction
All-or-none applies to one DML statement. It does not undo DML that ran earlier in the same transaction. Suppose an earlier insert succeeded and a later one fails. The later statement saves nothing. If you catch the exception, the earlier insert is still part of the transaction and will commit. The chapter on caught exceptions covers this in detail.
| Situation | Result |
|---|---|
| One statement, all records valid | All records saved |
| One statement, one record invalid | No records from that statement saved |
| Earlier statement succeeded, later statement fails and is caught | Earlier work stays, later statement saves nothing |
| Earlier statement succeeded, later statement fails and is not caught | The whole transaction rolls back |
What counts as a failure
Several things can fail a record and therefore the statement:
- A required field is missing.
- A validation rule rejects the record.
- A trigger calls
addErroron the record. - A field value is the wrong length or format.
- A duplicate rule or unique constraint blocks the save.
The failure does not need to come from your own code. A validation rule or trigger that an admin or another developer added can also cause the whole statement to fail.
Inspecting the failure
When the exception is caught, DmlException gives you per-record detail. This helps you find which record caused the whole list to fail.
List<Account> accts = new List<Account>{
new Account(Name = 'Good'),
new Account()
};
try {
insert accts;
} catch (DmlException e) {
for (Integer i = 0; i < e.getNumDml(); i++) {
System.debug('Row ' + e.getDmlIndex(i) + ': ' + e.getDmlMessage(i));
}
}getNumDml() returns the number of failed records. getDmlIndex(i) returns the position of the failed record in the original list. getDmlMessage(i) returns its error message.
Common errors
- Expecting valid records to survive. Developers sometimes assume the good records were saved before the bad one failed. With the default
insert, they were not. - Assuming a catch block cleans up earlier statements. The rollback covers only the failing statement. Earlier DML stays in the transaction.
- Ignoring errors from code you do not own. A new validation rule can turn a working bulk insert into one that fails entirely. Wrap bulk DML in
try/catchand log the row-level errors.
Exercise
A method runs insert new Account(Name = 'First');, then runs insert new List<Account>{ new Account(Name = 'Second'), new Account() }; inside a try/catch that catches DmlException. The second statement fails because the last record has no Name. After the transaction ends, which accounts exist?
Show answer
Only First. The second statement is all-or-none, so Second is rolled back along with the invalid record. The catch block handles the exception, so the transaction is not rolled back and First is committed.