+ Start a Discussion
Marty LavenderMarty Lavender 

New case created when including Case Thread ID

I have a VF Email template that is using a custom formula field to grab the thread ID for a case.

An example of my thread ID is: ref:_00D708Lra._50070koi90:ref

The formula that I am using to generate the thread ID is:

"ref:_00D"&MID(Id,4,1)&"0"&RIGHT($Organization.Id, 4) &"._"& LEFT(Id,4)&"0"&RIGHT(Id,5) &":ref"

I tested by sending a blank email with the thread ID in the subject and the body of the message. No case is opened by SalesForce, which is good as there shouldnt be a new case opened, but the email is never added to the case as a comment. The email just sort of dissapears into nowhere.

Can someone please help me with this?

You will need to write a trigger on the Email Object and create a new comment manually based on the email's ParentId.  If you look at the Email objects and you see your replies but the ParentId is not set correctly it may be because your refId is incorrect. The format of the refId that we are using is ref:XXXX.XXXX:ref without the _ like you have.
Marty LavenderMarty Lavender
I edited the formula and now we are outputting the thread ID like your example: ref:00D708Lra.50076koi43:ref

How would I be able to see the email coming in? Would I need to write an APEX class that will grab that information upon delivery? If the email isnt being added to the existing case and its not generating a new case, where is it going at this point? 
If you use something like Workbench [1] or the Developer Console, you can just use SOQL to query the Email records.  I'd start with a query like:

select FromAddress, ParentId, Subject, TextBody from EmailMessage where ParentId = '500xxxxx'

and if that returns results then you are good to continue on with your trigger.  Your trigger will be something like:

trigger EmailToCaseReply on EmailMessage (after insert) {
     List<CaseComment> newComments = new List<CaseComment>();
     Set<String> emailAddresses = new Set<String>();

     for (EmailMessage email: trigger.new) {


     if (!emailAddresses.isEmpty()) {
          Map<String, User> userMap = new Map<String, User>();

          for (User user: [
               select Email
               from User
               where Email in :emailAddresses
          ]) {
               userMap.put(user.Email, user);

          for (EmailMessage email: trigger.new) {
               if (!userMap.containsKey(email.FromAddress)) {

               newComments.add(new CaseComment(
                    ParentId = email.ParentId,
                    CommentBody = email.TextBody,
                    IsPublished = true,
                    CreatedBy = userMap.get(email.FromAddress).Id //This line may not work

          if (!newComments.isEmpty()) {
               insert newComments;

NOTE: This code has not been tested and may contain typographical or logical errors.

The CreatedBy line when creating the new comment may not work.  If it doesn't, just omit it.  You may want to adjust / omit the userMap part if you want anyone to be able to create comments on the case.  Or adjust it to look at contacts if you want to make sure they have a valid contact record.

You'll want to add additional logic to make sure you don't get stuck in an auto-reply loop, but that's a problem to look into after your initial implentation.
Workbench link https://workbench.developerforce.com/login.php
Marty LavenderMarty Lavender
pcon thanks for all your help  I really appreciate it 

When running select FromAddress, ParentId, Subject, TextBody from EmailMessage where ParentId = '500xxxxx' I get an error returned stating:

TextBody from EmailMessage where ParentId = '50070koi90'
ERROR at Row:1:Column:73
invalid ID field: 50070koi90

Sorry if I am sounding completely ignorant in all of this but am I running this with 500xxxxx or am I replacing that with that specific part of the thread ID that is being generated?
Marty LavenderMarty Lavender
If I simple query using: 
SELECT FromAddress, ParentId FROM EmailMessage
i get no results returned. I would assume with a query that simple, I should get something back correct?
Correct, you should be getting something back.  The '500xxxx' above should be the full 15/18 character Id of the case.  Have you replied back to an email with the new reference id?  If you have and you do not have any data in there, you'll want to create debug logs (setup -> monitoring -> debug logs) for your email handler user and verify that the data is actually coming in.
Marty LavenderMarty Lavender
So it would appear that the emails were in fact getting attached to the case once we generated the Thread ID. These emails were being attached to the case under the Email Related Field.
So with the case Id you see the Email Related Feild, can you query the EmailMessage object and see if you have any data for that ParentId?