function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mikeafter6mikeafter6 

Custom Controller returns 1 DMLException at a time; not returning all errors (like STD Controller)

I have 2 validation rules set up.  #1 for first name.   #2 for last name.    Assume that both fields have validation errors when I click the 'save' action.

 

Both errors appear on the same page when I use a standard controller.  However, my custom controller only returns the first DMLException error.  So, when both errors occur, only the error for the first-name field displays. If I fix the  first-name field and press 'save', then the last-name error appears.

 

 

public class catchMultipleDMLExceptionErrors {

public Person__c p { get; set; }

public catchMultipleDMLExceptionErrors () {
this.p = [Select id, first_name__c, last_name__c from person__c limit 1];
}

public PageReference save() {
try {
update p;
} catch (DMLException dmle) {
ApexPages.addMessages(dmle);
return null;
}
return null;
}
}

 

In other languages, the exception can be an array of exceptions (so in this case both the first-name & last-names are represented by the variable dmle.) 

 

I'm expecting both errors to be there, but they aren't.  How do I grab all the validation errors at the same time?

Thanks.

-Mike

 

sfdcfoxsfdcfox

The DMLException object does contain an array, but from what I have gathered through testing, it appears that there is only one error per failed record. This seems to be a bit of a limitation of the language, in my opinion. Try submitting a case to Developer Support and see if they have a solution that works. The documentation seems to suggest that multiple fields can be marked for failure at once, but I just tried that and it ever only returned one field at a time for failures.

 

As a workaround, try using the standard controller, then add your logic as an extension...