When a DML operation on a list of records of a concrete object type, e.g. Account
fails, calling getDmlFields
on the thrown DMLException
returns the fields that caused the failure, as described in the docs.
But when a DML operation on a List<SObject>
fails, getDmlFields
returns null.
Here's an example:
force apex <<EOF
try {
insert new List<Account>{new Account()};
} catch (DMLException e) {
for (Integer i = 0; i < e.getNumDml(); i++) {
System.debug('FAILED INSERTING ACCOUNT');
System.debug('ERROR FIELD NAMES');
System.debug(e.getDmlFieldNames(i));
System.debug('ERROR FIELDS');
System.debug(e.getDmlFields(i));
}
}
try {
insert new List<SObject>{new Account()};
} catch (DMLException e) {
for (Integer i = 0; i < e.getNumDml(); i++) {
System.debug('FAILED INSERTING SOBJECT');
System.debug('ERROR FIELD NAMES');
System.debug(e.getDmlFieldNames(i));
System.debug('ERROR FIELDS');
System.debug(e.getDmlFields(i));
}
}
EOF
In both cases, a validation rule on the Type
field causes the insert to fail. The call to getDmlFieldNames
returns Type
in both cases, but in the second case, the call to getDmlFields
returns null.
15:41:36.15 (39209292)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:new
15:41:36.15 (39398837)|CODE_UNIT_FINISHED|Validation:Account:new
15:41:36.15 (40276287)|EXCEPTION_THROWN|[2]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Type is required for Referral Accounts: [Type]
15:41:36.15 (40838118)|USER_DEBUG|[5]|DEBUG|FAILED INSERTING ACCOUNT
15:41:36.15 (40861711)|USER_DEBUG|[6]|DEBUG|ERROR FIELD NAMES
15:41:36.15 (40965320)|USER_DEBUG|[7]|DEBUG|(Type)
15:41:36.15 (40987483)|USER_DEBUG|[8]|DEBUG|ERROR FIELDS
15:41:36.15 (41104191)|USER_DEBUG|[9]|DEBUG|(Type)
15:41:36.15 (43357699)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:new
15:41:36.15 (43515984)|CODE_UNIT_FINISHED|Validation:Account:new
15:41:36.15 (44175767)|EXCEPTION_THROWN|[13]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Type is required for Referral Accounts: [Type]
15:41:36.15 (44572412)|USER_DEBUG|[16]|DEBUG|FAILED INSERTING SOBJECT
15:41:36.15 (44591105)|USER_DEBUG|[17]|DEBUG|ERROR FIELD NAMES
15:41:36.15 (44656270)|USER_DEBUG|[18]|DEBUG|(Type)
15:41:36.15 (44676099)|USER_DEBUG|[19]|DEBUG|ERROR FIELDS
15:41:36.15 (44719764)|USER_DEBUG|[20]|DEBUG|null
Is this expected behavior or a Salesforce bug?
getDmlFields
orgetDmlFieldNames
. Puzzling...getDmlFields
and names, there was just 1 row, with blank/null values.