When I send an email to a recipient from a case within Salesforce, the recipient's email subject contains a unique code generated by Salesforce. This code is used by Salesforce then for routing the response back to the case. Is there a way to get the unique reference code from within Salesforce (on the case object record), i.e., is there a field that stores this code? I would like to be able to attach emails to cases using this code.

2 Answers 2


This information is not stored in a field but it can easily be calculated in a formula or in Apex.


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


public static String ThreadId(String caseid)

        string Case_ThreadID = '_' + UserInfo.getOrganizationId().left(4) + '0' ;
        Case_ThreadID = Case_ThreadID + UserInfo.getOrganizationId().mid(11,4) + '._';
        Case_ThreadID = Case_ThreadID + caseid.left(4) + '0' + caseId.mid(10,5);

        return Case_ThreadID;

I used this in an e-mail template and my code finally looked like this:

ref:_{!LEFT($Organization.Id, 5)}{!MID($Organization.Id, 12, 4)}._{!LEFT(relatedTo.Id,5)}{!MID(relatedTo.Id,10,6)}:ref

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .