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