Our business has a lot of customized validation criteria and it can change and be very complex. There is no easy way to use required fields for all users to prevent validation errors.

I have created a dynamic list that allows managers and users to quickly update opportunities in mass. However, I am struggling with the handling of errors.

Is there a way I can test for validation issues when the list loads and notify the users before they even try to save the record? (Important note: it is possible that a record passes validation for one user but not another user. So a specific user could put in data that will make another user experience a validation rule error.)

Or does anyone have a method they use to let users know which record and which error a record may have occurred when saving a list of records?


2 Answers 2


You'd have to actually "try it out." You could do something like this in your code:

<apex:page controller="..." action="{!prevalidate}" ...

public void prevalidate() {
    Savepoint sp = Database.setSavePoint();
    Database.SaveResult[] results = Database.update(records, false);
    for(Integer index = 0, size = results.size(); index < size; index++) {
        Database.SaveResult result = results[index];
        if(!result.isSuccess()) {
            // handle errors here, if you like.
    Database.rollback(sp); // Roll back all changes

If the load delay would be unacceptable, you could also do it asynchronously using apex:actionFunction or something.

  • wouldn't this just tell me there are errors but not give me the record id? database.saveresult from what I understand only gives you an id when you have success, but is null on fail. So if I want to know which record is failing, don't i have to add some mapping and go through some lists to know which record, or is there an better way. I thought about doing one record at a time with a catch try block but I really don't want to go that route in case there could be a lot of records being updated and I have many update triggers (even if I did not, still probably bad idea) Jun 26, 2017 at 17:39
  • 1
    @disasterlooms records[index] will match results[index]. This is why I used a list and iterated over it by index instead of using a for-each loop.
    – sfdcfox
    Jun 26, 2017 at 17:45
  • got it. Will try that. I do have another question. I see you have records on action that runs on page load, but I am loading the page, then a search will determine 'records'. In this case, I would need to run the action prevalidate after I pull the records, or I could just call it from my controller. Am I on the right track? Jun 26, 2017 at 17:51
  • @disasterlooms yes, you can perform the action on the records you search by. You're definitely on the right track.
    – sfdcfox
    Jun 26, 2017 at 17:54

Here is how I solved this issue. I choose not to go with a rollback in this narrow situation. I instead displayed the error in page:messages. Here is what I did.

 List<Database.SaveResult> updateResults = Database.update(opps, false);
            for(Integer i=0;i<updateResults.size();i++){
                if (updateResults.get(i).isSuccess()){
                    system.debug('success id '+ opps.get(i).Name);
                    numsuccess += 1;

                }else if (!updateResults.get(i).isSuccess()){
                    // DML operation failed
                    Database.Error error = 
                    String failedDML = error.getMessage();
                    opps.get(i);//failed record from the list
                    system.debug('Failed ID '+opps.get(i).Name); 
                    system.debug('Message '+ failedDML);
                    errormessages += 'Name: 
                    '+opps.get(i).Name+'<br/>'+'Error: '+ failedDML+ '<br/>
                    numerrors += 1;


    system.debug('number of errors '+numerrors+ ' successes '+numsuccess);
    //Relay the number of successes
    if(numsuccess > 0){
          ApexPages.message(ApexPages.severity.Info,numsuccess+ ' record(s) 
          were successfully saved'));

    //Get the number of errors, if more than one then show the error message
    if(numerrors > 0){
         ApexPages.message(ApexPages.severity.ERROR,'There were 
         '+numerrors+' errors.Below are the records and'+
      ' reason(s) for error '+'<br/><br/>'+errormessages ));            

   return null;


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .