3

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);

}
7
  • The validation messages not triggering may not be down to an exception, although they well could be. I'd also recommend interrogating the debug logs to see what's going on. Nov 9, 2012 at 15:22
  • The debug logs say that a validation rule failed but there is no error message displayed. The comment is not added as expected but no error message is displayed. If I remove the database/dml options and use just the INSERT, it works fine (but no notification is sent).
    – PepeFloyd
    Nov 9, 2012 at 15:47
  • Is the validation rule configured to display the message next to a field, rather than on top of the page. Maybe why it can't display the message ? Nov 9, 2012 at 16:31
  • Is it standard validation or custom? Nov 9, 2012 at 17:35
  • @techtrekker: This validation rule displayed before I added the DMLOptions. So I don't think it's related to where it's configured to display.
    – PepeFloyd
    Nov 9, 2012 at 21:52

2 Answers 2

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);
   }
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
  )
);
}
3
  • usually you can just catch the DML Exception and do nothing since the system automatically displays most validation errors in the page message components (you're will repeat the same issue), but you'll still want to catch other types exceptions to prevent them in a user frindly fashion , see gist.github.com/4048738 for an example Nov 9, 2012 at 22:30
  • Thanks guys. Apologies for not replying. I will try this when I get a chance and provide feedback.
    – PepeFloyd
    Nov 15, 2012 at 22:48
  • @MohithKumar: I am using that tag in my VF page. Unfortunately, this didn't work. I believe it's related to the database method instead but I cannot understand why. This works if I remove the database options.
    – PepeFloyd
    Nov 19, 2012 at 10:37

You must log in to answer this question.

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