Find your content:

Search form

You are here

Database DMLOptions and Apex Error Messages

 
Share

We found an issue when portal users leave comments on our cases. Email notifications to the case owners were not being sent. So I did change the code to use the setoptions method.

The problem is that now, validation messages (validation rules triggered) are not showing on the page. Any idea how to fix this? Thanks in advance.

List<CaseComment> toInsert = new List<CaseComment>();

for (CaseComment newCom : newComments) {

    if (newCom.CommentBody! = null)  {
        newCom.parentId = cId;
        newCom.IsPublished = true;
        toInsert.add(newCom);                        
    }
}

try  {

    Database.DMLOptions dlo = new Database.DMLOptions();
    dlo.EmailHeader.triggerUserEmail = true;
    database.insert(toInsert, dlo);   

} catch(DmlException ex) {

    ApexPages.addMessages(ex);

}

Attribution to: PepeFloyd

Possible Suggestion/Solution #1

Can you try this if you are using apex pagemessage tag?`

<apex:pageMessages id="apexmsg"/>

method code

catch(DMLException e1) {
ApexPages.addMessages(e1);
 }   catch(Exception e2) {
// uh-oh, something else happened bad, better show it to the user
ApexPages.addMessage(
  new ApexPages.Message(
      ApexPages.Severity.ERROR
    , 'Unknown exception while inserting your list of stuff.  Please contact support. Exception: ' + e
  )
);
}

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

I ended up doing this:

if (newCom.CommentBody!=null)
        {
            newCom.parentId=cId;
            newCom.IsPublished = true;

            Database.DMLOptions dmo = new database.DMLOptions();
        dmo.EmailHeader.triggerUserEmail = true;
        newCom.setOptions(dmo);
            toInsert.add(newCom);


        }
    }

   try{

 Insert ToInsert;  
   }
catch(DMLException ex){

    ApexPages.addMessages(ex);
   }

Attribution to: PepeFloyd
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4239

My Block Status

My Block Content