Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.