function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Flora FuFlora Fu 

How to include the case email thread into an email alert?

Hi Team,

Could you advise how to include the case email thread into email alert?

When there is a new email received by a case, SF has a standard function to send out a notificaiton to case owners, and in that notification it includes the customer's new email. We just need the same notification to be sent to another case related user besides the case owner, and we've set up a workflow rule to trigger an email alert. Howver, the merge field {!Case.Email_Thread} don't work in the email template and as confirmed with SF support this is not possible through standard configuration as today.

Would be appreciated if anyone can help with this by coding, thanks in advance.
PratikPratik (Salesforce Developers) 
Hi Flora,

Please refr to this post:

Flora FuFlora Fu
Hi Pratik, Thanks for your reply, but it seems the post you mentioned doesn’t for my case. I want to include the customer’s whole email chain (email message date, subject, text body..etc.) into the email alert, not only the case thread ID. I’ve tried with the following merge fields but they all don’t work for WF &Email alert. Would be appreciated if you can help more. ☺ {!EmailMessage.FromAddress} {!EmailMessage.MessageDate} {!EmailMessage.ToAddress} {!EmailMessage.CcAddress} {!EmailMessage.Subject} {!EmailMessage.TextBody} Regards, Flora
Ido Greenbaum 10Ido Greenbaum 10
Hey Flora, 

We have an issue with the Case.Email_Thread not populating as part of a workflow rule, which SalesForce support admitted as a known issue. 
I am seeking for a workaround, and wonder if you managed to overcome this issue somehow. 

Thank you, 
Ingram SmariusIngram Smarius

Same here, I've tried using all of the ones you included and also {!Case.Email_Thread} and this just doesn't work. 

I check several other cases in which it was mentioned that using {!EmailMessage.TextBody} was not working but if you used an HTML template and included {!EmailMessage.HtmlBody} it would work - this doesn't work either. 

Suprisingly when you open a case with SF their HTML template seems to work just fine because the entire email thread is in each response you get back. Would be nice they included their template as an example.

Ido Greenbaum 10Ido Greenbaum 10
Hi Ingram, 

I worked with SalesForce Support and they clarified that 'Case.Email_Thread' merge field is not supported in a WorkFlow rule. 
I was able to overcomet this limitation with a code solution (Apex Class which queried the EmailMessage object, along with an appropriate VisualForce controller to sort the Email Messages into a thread, and a VisualForce Email Template to add the Email Thread in our Email Notification). 

Let me know if you want me to share this solution. 

Thank you, 
Doug Bennett 4Doug Bennett 4
Hi there Ido, I would love it if you could share a solution to this.  I've got many SF users here that are frustrated with the inability for SF to capture the entire HTML response into the Case.Email_Thread.  I'm in the process of experimenting whether if I can update the emailmessage.textbody with the emailmessage.htmlbody upon each send in Process builder (I'm assuming the Case.Email_Thread gets it's data from the emailmessage.textbody) . Many thanks for any help you can lend.

Ido Greenbaum 10Ido Greenbaum 10
Hi Doug, 
I tried the merge field {!EmailMessage.HtmlBody} as part of a Custom HTML Email Template, and it doesn't seem to work - no text is merged. 

In regards the code solution, please find below. 

Apex Class to query EmailMessage:
*notice we are extracting only the EmailMessages in which the Case Contact was included as To, Cc, or Bcc. 
public with sharing class CaseEmailExtension {

public ID csId {get; set;}  // comes from assignTo on component 

public CaseEmailExtension() { }
private final Case currentCase;

public CaseEmailExtension(ApexPages.StandardController currentcase) {
    this.currentCase = (Case)currentcase.getRecord();

  public List<EmailMessage> getSortEmails(){
  String contactEmail = [SELECT Id, ContactEmail FROM Case where Id=:csId].get(0).ContactEmail;
  contactEmail = '%'+contactEmail+'%';
    return this.csId == null
        ? new List<EmailMessage>()  // handles UI preview use case
        : [SELECT Id, FromAddress, ToAddress, BCCAddress, CcAddress, MessageDate, Subject, Incoming, TextBody, HtmlBody, CreatedBy.Name   
            from EmailMessage where ParentId =: this.csId AND(FromAddress LIKE :contactEmail OR ToAddress LIKE :contactEmail OR BCCAddress LIKE :contactEmail OR CcAddress LIKE :contactEmail)
            order by MessageDate DESC ];

And the matching Test Class: 
Public class CaseEmailExtension_Test{

     static testMethod void EmailExtension() {
      Contact TestCon= new Contact();
      Insert TestCon;
      Case Testcase= new Case();
      Insert Testcase;
      EmailMessage TestEmail = new EmailMessage();
       ApexPages.StandardController sc = new ApexPages.StandardController(Testcase);
       CaseEmailExtension Objcon= new CaseEmailExtension();
       CaseEmailExtension Obj= new CaseEmailExtension(sc);
       Obj.csId= Testcase.Id;

A VisualForce Component: 
<apex:component controller="CaseEmailExtension" access="global">
    <apex:attribute name="caseId" type="Id"  description="Case Id" assignTo="{!csId}"/>    
            <apex:repeat value="{!sortEmails}" var="email" rows="1">
From: &nbsp; <apex:outputText value="{!email.FromAddress}"/><br></br>
Sent: &nbsp; <apex:outputText value="{0,date,dd'.'MM'.'yyyy HH:mm:ss z}">
    <apex:param value="{!email.MessageDate}" /></apex:outputText><br></br>
To: &nbsp; <apex:outputText value="{!email.ToAddress}"/><br></br>
Cc: &nbsp; <apex:outputText value="{!email.CcAddress}"/><br></br>
Subject: &nbsp; <apex:outputText value="{!email.Subject}"/><br></br>
<apex:outputPanel rendered="{!email.HtmlBody!=null}">
                        <br><apex:outputText value="{!email.HtmlBody}" escape="false"/></br>
<apex:outputPanel rendered="{!email.HtmlBody=null}">
                    <br><apex:outputText value="{!email.TextBody}" style="white-space:pre;"/></br>

And a VisualForce Email Template: 
<messaging:emailTemplate subject="{!relatedTo.Subject}" recipientType="Contact" relatedToType="Case">
<messaging:HtmlEmailBody >
        <c:CaseEmailExtension caseId="{!relatedTo.Id}"/>

We are using this Email Template as part of a WorkFlow Rule, which Emails the Case Contact after we had to reply back to the case for 3 days. The Email Template includes the whole Email Thread, so the customer has the full context of the handled case. 

Hope this helps. 
Let me know if you need any more help. 


Gabriel McGinnGabriel McGinn

@Ido Greenbaum 10 thank you for sharing your solution.