I've been looking around but haven't found a good explicit answer for handling when multiple validation rules fail simultaneously.
Let's say I have a custom object called MyObject__c with two different validation rules.
The first validation rule will be on a number field called SampleQuantity__c. If SampleQuantity__c < 10, it'll throw an exception saying "Your minimum quantity is 10"
The second validation rule will be on another number field called SampleHoursSpent__c. If SampleHoursSpent__c > 40, it'll throw an exception saying "You have spent the maximum number of hours per week. Please update overtime field."
I'll have something similar to the following when I update my record.
List<MyObject__c> myObjList = getObjects();
try {
update myObjList;
} catch (DMLException ex) {
/* this line will get both error messages if both of them fail simultaneously */
ApexPages.addMessages(ex);
/* however, if i try to get the number of dml exceptions with a loop,
* there is only 1
*/
for (Integer i = 0; i < ex.getNumDml(); i++) {
System.debug(ex.getDmlMessage(i));
}
}
Why can I not see all the DML messages when I try to iterate over them, yet I am able to get them through ApexPages? Shouldn't the behavior be the same?
I've also tried browsing SFDC known issues, but couldn't find something that is related to this issue.