2

We have a requirement to use an email template to reply to a Case, save the email in the Case's Activity History, and update the Case's status. I have the email being sent and the status updating, but the email is only saved on the associated Contact, not the Case.

How can I get the email to also show on the Case? We are using the service console and Case Feed view, and the code is being invoked through publisher action that calls a VF page.

Current code:

List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage theMessage = new Messaging.SingleEmailMessage();
theMessage.setTemplateId(emailTemplateId);
theMessage.setTargetObjectId(this.myCase.ContactId);
List<String> toAddress = new List<String>();
toAddress.add(this.myCase.Contact.Email);
theMessage.setToAddresses(toAddress);
theMessage.setWhatId(this.myCase.Id); 
theMessage.setSaveAsActivity(true); // Only saves to TargetObjectId
messages.add(theMessage);    
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
4
  • Do you include the case Id anywhere in the body of the email (perhaps as a link)? Email to Salesforce if I recall will search the body of the email for record Ids and associate the email as an activity to those records. I don't know that it behaves the same for an email sent from apex though. Nov 21 '14 at 15:05
  • Yes, the Case is linked as the WhatId, through setWhatId().
    – Mike Chale
    Nov 21 '14 at 15:10
  • 1
    I tried your code in a publisher action in my DE org, and an activity was created for both the Contact and Case. I assume myCase.Id is never blank in your case. Nov 21 '14 at 16:58
  • Can you post that as an answer so I can accept it? I also got it to work in a different org so it's something else.
    – Mike Chale
    Nov 21 '14 at 17:09
1

you can simply create this , regarding to your code you would need to instantiate the EmailTemplate object and load it into your instance variable with all necessary info

http://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_emailtemplate.htm

EmailTemplate ET = new EmailTempalte([Select id,Body, HtmlValue,Subject FROM EmailTemplate Where Id =: emailTemplateId]);

this below saves an email under emails related list in the case

 EmailMessage em = new EmailMessage();
               em.subject = ET.subject;
               em.body = ET.body;
               em.HtmlBody = ET.htmlValue;
               em.ParentId = case.id;
               insert em;

only issue on this method is that all merge fields will be empty but you can easily replace the merge field placeholder if you have the values in your code at hand with the string.replace(x,y) method assuming htmlval is a string and you have the contact firstname in mycase.contactFirstname stored use htmlval.replace('{!Contact.firstName}',mycase.contactFirstname); before inserting em

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.