Sub topic 9
Handling DML failures with DmlException
This chapter assumes you know basic Apex syntax, SOQL and DML statements, and how try and catch work. It covers what a DmlException tells you and how to read it for each failed record.
When DML throws
A DML statement such as insert, update, or delete throws a DmlException if any record fails. Common causes are missing required fields, validation rule failures, and failed triggers. By default the whole statement is all-or-nothing. If one record in a list fails, none of the records from that statement are saved.
The exception carries one entry for each failed record. Do not stop at getMessage(). It returns a single summary string and can hide which record failed or which field caused the failure.
Reading each failure
DmlException has methods that report on each failed record by its index:
| Method | Returns |
|---|---|
| `getNumDml()` | Number of failed records |
| `getDmlIndex(i)` | Position of the failed record in the original list |
| `getDmlFieldNames(i)` | Fields involved in failure `i` |
| `getDmlMessage(i)` | Error message for failure `i` |
| `getDmlType(i)` | `StatusCode` value, such as `REQUIRED_FIELD_MISSING` |
The following example inserts an Account without a Name and prints each failure.
try {
insert new Account();
} catch (DmlException e) {
for (Integer i = 0; i < e.getNumDml(); i++) {
System.debug(e.getDmlFieldNames(i) + ': ' + e.getDmlMessage(i));
}
}The loop variable i indexes the failures, not the records in your list. To find the original record, call getDmlIndex(i) and use that value as the list index.
List<Account> accts = new List<Account>{ new Account(Name = 'Valid'), new Account() };
try {
insert accts;
} catch (DmlException e) {
for (Integer i = 0; i < e.getNumDml(); i++) {
Account failed = accts[e.getDmlIndex(i)];
System.debug('Record ' + e.getDmlIndex(i) + ' (' + failed.Name + '): '
+ e.getDmlType(i) + ' - ' + e.getDmlMessage(i));
}
}Here nothing is saved, including the valid Account, because the statement is all-or-nothing.
Choosing a response
Once you have caught the exception, you have three common options:
- Rethrow it so the caller or the platform handles it.
- Wrap it in a custom exception with more context. A later chapter covers this.
- Log the details and continue, but only if the rest of the transaction is safe to commit.
Catching a DmlException and doing nothing hides data problems. If you catch it, either report the per-record details or rethrow.
Common mistakes
- Logging only
e.getMessage()and losing the per-record detail. - Using the failure index
ias the position in your original list instead of callinggetDmlIndex(i). - Assuming valid records are saved when others fail. With a plain DML statement, they are not.
- Catching
ExceptionwhereDmlExceptionwould do, which also hides unrelated bugs.
Exercise
You run insert accts; on a list of 10 Accounts, and one has no Name. In the catch block, e.getNumDml() returns 1. How do you get the Account that failed, and how many of the 10 Accounts were saved?
Show answer
Call e.getDmlIndex(0) and use the result as the index into accts, as in accts[e.getDmlIndex(0)]. None of the 10 Accounts were saved, because a plain insert statement is all-or-nothing.