System.DmlException: meaning and how to fix it
Quick answer
System.DmlException: A DML statement failed. This is a wrapper: the real reason is the status code after "first error:". Start with: Find the status code after "first error:" and look it up in this list.
Typical message: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Cause
A DML statement failed. This is a wrapper: the real reason is the status code after "first error:".
How to fix System.DmlException
- Find the status code after "first error:" and look it up in this list.
- The row number is zero-based: row 0 is the first record in the list.
- Use Database.insert(records, false) and inspect each SaveResult to see every failure instead of only the first.
Database.SaveResult[] res = Database.insert(records, false);
for (Database.SaveResult r : res) {
if (!r.isSuccess()) System.debug(r.getErrors()[0].getStatusCode() + ': ' + r.getErrors()[0].getMessage());
} Have a different message? Paste it into the error decoder, which matches it against 45 common Salesforce errors in your browser.
Frequently asked questions
What does System.DmlException mean in Salesforce?
A DML statement failed. This is a wrapper: the real reason is the status code after "first error:".
How do I fix System.DmlException?
1. Find the status code after "first error:" and look it up in this list. 2. The row number is zero-based: row 0 is the first record in the list. 3. Use Database.insert(records, false) and inspect each SaveResult to see every failure instead of only the first.
More Apex runtime errors
SFDX Talk is unofficial documentation. It is not affiliated with, endorsed by, or sponsored by Salesforce, Inc. Message text varies slightly by release and context, so confirm against the official page before relying on it.