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

How do I track emails that I send from within a case

Hi guys,

How can I keep records of emails that I send to clients from within the case record. So I am able to send an email to a client from within the record, fine. None of the replies are being added to the record.

Am I missing something? 
Prashanth SamudralaPrashanth Samudrala
Hey Nevin, It could be due to a missing thread ID - Read more - 

Review the following to ensure that Email-to-Case has been set-up correctly for customer Email responses to be attached to and viewed from the case.

1. The Thread ID needs to be added to either the Subject of the Email, the body of the Email or both.  It is recommended that it be added to both.  This will give Salesforce a better chance of locating the case.  Also, if a customer removes the Thread ID from one or the other there is a back up.  To add the Thread ID follow the steps below:

Your Name | Setup | Customize | Cases | Email to Case

2. When replying to a customer always change the address from the Owner to the Email to Case support address.  If this is not changed then the response from the customer will be sent to the owner of the case and will not be added to the Email Thread.

3. Add the Email Related list to the Case Layout.  When a customer replies to an email that has the Thread ID the Salesforce will identify the case via the Thread ID, the Case Owner will be sent a notification, and the Email will be added to the Email Related List.  To add the Related List to the Case Layout:

Your Name | Setup | Customize | Cases | Page Layouts

     - Select the Layout in question and select edit
     - Click on Related Lists
     - Drag and Drop "Emails" to the Page Layout
     - Save

4. A common issue when testing Email-to-Case before deployment or when testing in the Sandbox the support address has not been forwarded to the Email Services Address. If you have not already done this then the replies are not hitting Salesforce and cannot be attached to the case.  Contact your Email Administrator to have them forward the emails to the Services Address.

Some more additional help -

Vikas gupta 24Vikas gupta 24
Hi Nevin, 

use thread id in the reply email so sfdc automaticallly add those emails to case.
for further details user following link-

Thanks, Vikas
Thanks for the reply guys,

I have already setup Email-to-Case and it works fine. My question is that if I reply to an email from within the Case object the email that is sent out is from my email address, because that is the email address that is associated to my user account and I am the Case record owner.

So as a result of this do I need to manually add the thread each time I reply to an email or want to collaborate with a client, in order for the emails to be logged in that record?
Vikas gupta 24Vikas gupta 24
In your email template either in subject or description you need to add thread id so every time you send an email will automatically have thread id.
No manual addition is needed. 
But do I need to setup my personal email address in the email to case so that the incoming email gets sent to Salesforce? Or is it as simple as just adding the Thread id to the email template?
Amit GhadageAmit Ghadage
Hi Nevin,
May be you are not inserting email in EmailMessage Object.

after inserting email to Case add folloeing code snippet to it.
EmailMessage emailMessage = new EmailMessage();
                emailMessage.FromAddress = emailAddress;
                emailMessage.subject = email.subject;
                emailMessage.ParentId = c.Id;
                emailMessage.TextBody  = email.plainTextBody;
insert emailMessage;

add your logic to extract parentId(i.e Case id) so that email will be attached to appropriate Case.

Besy Regards,
Amit Ghadage.
Vikas gupta 24Vikas gupta 24
Yes! just need to add thread Id in email template only. Please give it a try. 
Hi Amit,

I don't understand. Where do I add this code? To the cases trigger? 
What does this trigger do?  Also What do you mean by "after inserting email to Case"? Do you mean to setup the email to case to include my personal email address?

I have already setup email to case and added a generic email address. This is only to be used when clients want to submit a case via email. If a user is replying to the clients email, I want the From email to be the users email address.

Does your solution above cover this? 
Amit GhadageAmit Ghadage
Hi Nevin,
you have to write this code in your Email Service class.

Best Regards,
Amit Ghadage.
Hi Vikas,

It doesn't work. This is what I have done:
  • Created an email template
  • Added the Thread id to the subject and the email content page
  • Submitted a case by sending an email to the email-to-case email address that I have forwarding to salesforce.
  • Case was created on Salesforce
  • Replied to the email from the Email related list from within the case
  • Attached the email template which included the Thread ID
  • Received the email in the inbox of the email address associated to the client
  • Replied to that email
  • Email did not add to the case record in Salesforce
Hi Amit,

Ok, but what will the output of this be? Will the result be that any email I send from within a case record and replies to those emails will be recorded on the Salesforce record?
Hi Amit,
Do I add this as an Apex Class first and then add that Apex Class to the Email Service?
Amit GhadageAmit Ghadage
Hi Nevin,
Yes Incoming replies and outgoing emails of Case will be added to related list of Case.
Vikas gupta 24Vikas gupta 24
Hi Nevin, 

Can you please send me any generated thread id? Just want to check format.

Hey NevDev,

you want from address to be added in the email while replying means.
you can do it in 2 Ways.
1. using an organization-wide address add your email service address and verify and u can use your email Id and u can reply.
2. Go to standard object / Go to Buttons, Links, and Actions/search for email and open that at bottom there are predefined values / click new and add from address. after saving u will get from address in standard object email reply also and from google mail if u reply u need not change to address too. u can directly reply from there.

 If the From email to be the user's email address:
1. you can change your forwarding mail address to that email service address. then u can use user org Id. else
2. go through organization-wide address. it will work.
Anand kurubaAnand kuruba

global class Caseinboundmassage implements Messaging.InboundEmailHandler{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope env){
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        //how we can get from address 
        String conemail = email.fromAddress;
        //check contact have this email
        contact con = [SELECT id,Name FROM Contact WHERE email =: conemail limit 1];
        system.debug('Contact is'+con);
        if(con != null){
            //creat case
            Case cas = new Case();
            cas.contactid =;
            cas.Subject = email.subject; 
            cas.Origin = 'Email';
            cas.Status = 'New';
            cas.Priority = 'low';
            cas.Description = email.plainTextBody;
                insert cas;
                result.success = true;
            }Catch(DMLException e){
                system.debug('Following error message occured==== '+e);
                result.success = False;
              if(email.textAttachments != null)
            // Save attachments, if any
            for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
              Attachment attachment = new Attachment();
              attachment.Name = tAttachment.fileName;
              attachment.Body = Blob.valueOf(tAttachment.body);
              attachment.ParentId = cas.Id;
              insert attachment;
        if(email.binaryAttachments != null)
            for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
              Attachment attachment = new Attachment();
              attachment.Name = bAttachment.fileName;
              attachment.Body = bAttachment.body;
              attachment.ParentId = cas.Id;
              insert attachment;

            system.debug('Contact Not Exist');
        return result;

please choose Best answer