2

My trigger inserts a case comment when a timecard is entered. I want the case comment to be public and send an email to the case contact. It does make the comment public but doesn't send the contact an email notification. Although when I look at the actual comment, it has "public" and "Send Customer Notification" both checked off. The email is being sent out to the Case Team but not Case Contact though.

Am i missing something?

Note: I already have templates set up to send case notifications. I just want send it through code in my trigger. Only problem is, "c.IsPublished = TRUE;" does not seem to be working.

Trigger Code as is:

trigger copyTimeCard on SFDC_Service_Timecard__c (after insert) {
    // Use DML options to enable email delivery
    Database.DMLOptions dmlOpts = new Database.DMLOptions();
    dmlOpts.EmailHeader.triggerAutoResponseEmail = true;
    dmlOpts.EmailHeader.triggerOtherEmail = true;
    dmlOpts.EmailHeader.triggerUserEmail = true;

    for (SFDC_Service_Timecard__c timeCard : Trigger.new) {
           if(timeCard.Service_Notes__c != null){ //make sure timecard notes are not empty
               string s = timeCard.Service_Notes__c; //copy the notes to a string object           
               CaseComment c = new CaseComment(ParentId = timeCard.Case__c); //create a new case comment & link to Parent Case
               c.CommentBody = s; //insert the notes into the commentbody attribute of the case comment
               c.IsPublished = true; //make comment published

               database.insert(c, dmlOpts); //insert the case comment

           }
      }

}
3
  • How'd this work out for you @user988? Commented Mar 16, 2013 at 20:39
  • @user320 It didn't work for me. Basically What i'm trying to do is every time a timecard is entered -> enter a new case comment, make it public and send the contact the email. I've been able to do all of these things except for the fact that the notification email does not get sent out to the contact. But, it gets sent out to the case team. Which is weird. And I already a template I'm using.
    – user988
    Commented Mar 19, 2013 at 21:32
  • CaseComment.isPublished does not control anything about email delivery. It indicates whether the CaseComment is visible to customers in the Self-Service portal.
    – Mark Pond
    Commented Mar 20, 2013 at 0:53

4 Answers 4

1

Here is an alternative you can look at using workflow combinations: - Insert case comments using your trigger. - On insert of case comment, write a workflow to update a field (say a check box called "send email notification" on the case). - On the case, write a workflow email alert to notifying the contact when the "send email notifcation" is updated.

1
  • This is the workaround Salesforce support suggested I do, because apparently it could not be done through DML options.
    – user988
    Commented Apr 10, 2013 at 18:06
2
+50

Mohith is correct, you need to use the Database.DMLOptions class to achieve this behavior. Based on your example code:

trigger copyT on SFDC_Service_Timecard__c (after insert) {

    // Use DML options to enable email delivery
    Database.DMLOptions dmlOpts = new Database.DMLOptions();
    dmlOpts.EmailHeader.triggerAutoResponseEmail = true;
    dmlOpts.EmailHeader.triggerOtherEmail = true;
    dmlOpts.EmailHeader.triggerUserEmail = true;

    for (SFDC_Service_Timecard__c timeCard : Trigger.new) {
        string s = something.field__c //make a string from field              
        c.CommentBody = s; //insert a comment
        c.IsPublished = TRUE; //make comment published ***

        database.insert(c, dmlOpts); //insert the case comment (with the DML options)
    }
}

Take a look at the DMLOptions Properties documentation for more information about what each of the three EmailHeader values controls.

8
  • I did this. Did not work. weird
    – user988
    Commented Mar 19, 2013 at 22:52
  • it inserts the comment (which is public) but doesn't send out the email notification as it should.. hmmm
    – user988
    Commented Mar 19, 2013 at 23:13
  • Can you post the exact code which you are using in the trigger rather than the pseudocode? Something here doesn't add up. Does your Case have a ContactId value on it or does the Case have a SuppliedEmail value? Is the email address on the Contact valid? Is email bounce management enabled in the org? Did the Contact have an email bounce previously which now needs to be addressed?
    – Mark Pond
    Commented Mar 20, 2013 at 0:49
  • 1
    @user988 this sounds like a bug as the DML options is really the correct route, if you haven't already it'd be really helpful for everyone if you submitted it to SFDC support. Commented Mar 20, 2013 at 16:51
  • 1
    @Ralph SFDC Support says this cannot be done through DML options and suggested a 2-workflow workaround to update a checkbox on the case and another to send an email notification based on the checkbox
    – user988
    Commented Apr 11, 2013 at 16:50
2

I believe Send Customer Notification is an artefact of the web UI. If you are creating CaseComments and want to shoot off an email from a trigger, you could do it like this:

trigger CaseCommentNotification on CaseComment (after insert) {

  //only notify when comment is public
  Set<Id> caseIds = new Set<Id>();
  for (CaseComment caseComment : Trigger.new) {
    if (caseComment.IsPublished) caseIds.add(caseComment.ParentId);
  }

  //only notify when contact has an email
  List<Case> cases = [
    SELECT ContactId
    FROM Case
    WHERE Id IN :caseIds AND Contact.Email != ''
  ];

  //register an address in your org first
  OrgWideEmailAddress emailAddress = [
    SELECT Id
    FROM OrgWideEmailAddress
    WHERE Address = '[email protected]'
  ];

  //create a template in your org first
  EmailTemplate template = [
    SELECT Id
    FROM EmailTemplate
    WHERE DeveloperName = 'CaseCommentNotification'
  ];

  //be good and bulkify your trigger
  List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
  for (Case basketCase : cases) {
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    message.setOrgWideEmailAddressId(emailAddress.Id);
    message.setTargetObjectId(basketCase.ContactId);
    message.setWhatId(basketCase.Id);
    message.setTemplateId(template.Id);
    messages.add(message);
  }

  try {Messaging.sendEmail(messages);}
  catch (Exception e) {
    ApexPages.addMessages(e);
  }

}
2
  • there are two typos at the end of the cases query you have an extra ) and at then end of the OrgWideEmailAddress query you have a , instead of a ;
    – Aba
    Commented Oct 16, 2017 at 19:45
  • @Aba thank you! Commented Oct 17, 2017 at 12:38
1

www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_dmloptions.htm

Setting the property on the email header would solve the issue.There are two properties.The one for intrernal sfdc users and other for outside sfdc users.you set according to your business need

You must log in to answer this question.

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