1

I have a trigger written which is copying comments from a Custom Object (Timesheet) to the CaseComment object.. I am marking them as 'IsPublished = true'

The problem is that email notifications are not being sent to the Contact for any comments that are copied with this trigger. But any manually created comments are notified to the Contact.

How do I resolve this?

  • what in your setup causes CaseComment emails to be sent to the Contact? For example, using workflows? or using a switch in Case Settings? – cropredy May 9 '16 at 19:40
  • I think this is standard functionality .. Whenever you add a public case comment from the UI on any case, it sends out the communication – Maneesh May 9 '16 at 19:42
  • 1
    Please check: Case | Settings | Enable Case Comment Notification to Contacts; please check Workflows on Case and CaseComment. This is all configurable so there's really no 'standard functionality' – cropredy May 9 '16 at 19:47
  • Yeah but that's not my question – Maneesh May 9 '16 at 19:52
  • Maneesh -- just trying to understand how emails get sent, since you didn't post the trigger code, I'm assuming that emails are sent by either a workflow or the Case Setting and are not manually sent from Apex; Sorry if I misunderstood – cropredy May 9 '16 at 19:56
2

This may be your issue.

Personally, I find that the Setup | Case | Support Settings check boxes don't always do what you think they will do under all use cases - Web to Case, Email To Case, and direct user interaction with the OOB Case UI.

In one of my orgs, after some frustration, I did the following for a consistent Case Comment email experience

  1. Added two custom Checkbox fields on Case: HasNewPublicCaseComment__c and HasNewPrivateCaseComment__c
  2. Two workflows on CaseComment to set the corresponding fields on Case
  3. Workflow on Case with criteria HasPublicCaseComment__c = true.
  4. The Case workflow does the Email Alert using the merge field for the most recent case comment and resets the HasNewPublicCaseComment__ to false. Case email alerts have access to more email recipients than CaseComment email alerts

Notes:

  1. Assumes that CaseComments aren't uploaded for the same Case in bulk in one transaction
0

I think the problem lies in DMLOptions Email Header is not enabled while you are inserting your case comment in your trigger.

You should be doing something like this before calling insert operation on case comment instance

Database.DMLOptions opts = new Database.DMLOptions();
opts.EmailHeader.triggerAutoResponseEmail = true;
opts.EmailHeader.triggerOtherEmail = true;
opts.EmailHeader.triggerUserEmail = true;
Databse.insert(myComment,opts);

Edit:

After looking through comments, i googled and found this SFSE question which mentions this cannot be achieved. As a workaround,

  1. Insert case comments using your trigger.
  2. On insert of case comment, write a workflow to update a field (say a check box called "send email notification" on the case).
  3. On the case, write a workflow email alert to notifying the contact when the "send email notifcation" is updated.

Hope this helps.

  • Hi Vignesh, Below is my code ` if(!PublicComments.isEmpty()) { Database.DMLOptions opts = new Database.DMLOptions(); opts.EmailHeader.triggerAutoResponseEmail = true; opts.EmailHeader.triggerOtherEmail = true; opts.EmailHeader.triggerUserEmail = true; PublicComments.setOptions(opts); Database.insert(PublicComments); } ` I am getting an error saying "Error: Compile Error: Method does not exist or incorrect signature: [List<CaseComment>].setOptions(Database.DMLOptions) at line 88 column 14" – Maneesh May 9 '16 at 18:14
  • Sorry my bad. You should be using Database.insert by passing the casecomment instance and the dmloptions instance. Updated my answer. – Vigneshwaran G May 9 '16 at 18:27
  • Ok.. Did that and saved the trigger. But still no emails are coming through .. – Maneesh May 9 '16 at 18:35
  • Does it work for CaseComment object ? – Maneesh May 9 '16 at 18:36
0

I handled it using trigger only. Below is the code

 if(!PublicComments.isEmpty())
       {

         System.debug('Entering Bllock - Public Comments Not Empty');

         Database.insert(PublicComments);
         System.debug('TS Comments inserted successfully as Public Comments');
         System.debug('Starting Email Sending Loop');

         for(integer i = 0; i < PublicComments.size(); i++) 
         {
           Case newcase = [Select ContactID, casenumber from Case Where ID = :PublicComments[i].parentID]; // getting the Contact ID & CaseNumber
           System.debug('Newcase - ');
           System.debug(newcase);
           System.debug(newcase.casenumber);
           System.debug(newcase.ContactID);

           If (newcase.ContactID != null)  // there could be cases with no associated contact
           {

              Contact newcontact = [Select Email from Contact Where ID = :newcase.ContactID]; // get the email of the Contact
              System.debug('NewContact - ');
              System.debug(newcontact);             

              If (newcontact.Email != null)  // there might not be any email address for a Contact
              {
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.setSubject('A new Case Comment has been posted '+newcase.casenumber);
                message.setPlainTextBody('Case Number: '+newcase.casenumber+' - '+PublicComments[i].CommentBody);
                message.setToAddresses(new String[] { newcontact.Email });

                Messaging.sendEmail(new Messaging.Email[] {message});
              }
           }

        }

        System.debug('EXIT the block - Public Comments Not Empty');             
       }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.