+ Start a Discussion

How to replicate the email to case Reference ID and use it in cases and email templates

Create a formula field on the cases object called "Reference"


with the following formula


"[ ref:???????.????" & SUBSTITUTE( RIGHT(Id,11) , "0", "") & ":ref ]"


Replacing the ?? with the data from one of your own ref records.

The Red ?? you can copy directly from one of your own Ref records

The Blue ?? are the first 4 of the Case ID which doesn't change



Once you've created this field you can add it to workflow email templates on the subject line and it will basicly do what SF does when you email out from a case


You can also copy  this Reference when someone sends you an offline email and forward it to your support email with the Ref pasred in the subject line and it will attach that email to your case.


Hi Greg,

This is great and really helpful.

I think it can be further optimized so that it's completly data driven as follows:


"[ ref:" & LEFT($Organization.Id,4) & SUBSTITUTE(RIGHT($Organization.Id,11), "0", "" ) & "." & LEFT(Id,4) & SUBSTITUTE(RIGHT(Id,11), "0", "" ) & ":ref ]"





You two may have saved my sanity.  This works GREAT!!  Thanks!


This is great!

Gotta love the community and the power of Google!


I had to update this a bit to get it to work (it was missing some characters).  Here is what I used:


"ref:_" & LEFT($Organization.Id,5) & SUBSTITUTE(RIGHT($Organization.Id,11), "0", "" ) & "._" & LEFT(Id,5) & SUBSTITUTE(RIGHT(Id,11), "0", "" ) & ":ref"

Jesse CreechJesse Creech
How exactly do you enter this into the email alert subject line? My subject line says: ["ref:_" & LEFT($Organization.Id,5) & SUBSTITUTE(RIGHT($Organization.Id,11), "0", "" ) & "._" & LEFT(Id,5) & SUBSTITUTE(RIGHT(Id,11), "0", "" ) & ":ref"]

I put the reference in the email alert right after a line of text... should I put quotes around the brackets too?
Jesse CreechJesse Creech
Correction: email alert = email template.... also, if someone replies to this email that contains the reference in the subject line, will SF add it to the emails related list on the case page?
Harold CarlsonHarold Carlson
This field is now available to add to email templates: https://help.salesforce.com/apex/HTViewSolution?urlname=Why-is-the-email-to-case-generating-new-cases-from-reply-emails-1327108630931&language=en_US  However it is not an exposed field, so it can't be added as a field to a case layout page.
Please note that with Winter '16, the shortened reference ids will no longer work -- they need to be at least 5 char of org id and 6 char of case id.  We found this out the hard way.  SFDC's official (and arrogant) response is below:
Firstly, we don't recommend and support (never did) custom formulas to generate Case reference ids. The reason for that as the reference id format is subject to change anytime without notice.

Secondly, the root cause here is that post Winter'16 we have a more strict reference id format. Your existing reference id (generated by their their custom formula) format is incorrect and therefore is not parsed as it did prior to Winter'16.

To add why it worked before and not now?

Before Winter'16:
The org had a broken ref ID, we had an un-strict way of parsing ref IDs, coincidentally, everything worked. It's possible we had a parsing error before and with Winter'16 we have fixed that too.

After Winter'16:
Your org still has the broken ref ID, but we no longer parse them, because there is no expectation that we would, and the org ID is incorrect. And we no longer parse it due to a change on our end where we tightened the org ID requirement.

For your reference you can look at below example on how reference it is generated - WE DON'T SUPPORT THE FORMULA BELOW AND FORMAT CAN CHANGE AT ANY TIME:

Organization ID Format:

Case ID Format:

Acceptable Thread ID Formats:
(1) ref:_00DXXyyyyyyyyyy._500AAbbbbbbbbbb:ref
(2) ref:00DXyyyyyyyyyy.500Abbbbbbbbbb:ref

​For example (example org id):
Organization ID = 00D50000000IQwR
Case ID = 5005000000PQo5L

Thread ID = ref:_00D50IQwR._50050PQo5L:ref

Custom Formula:

"ref:_"&LEFT( $Organization.ID,5)&SUBSTITUTE(RIGHT($Organization.ID,10),"0","")&"._"&LEFT(Id,5)&SUBSTITUTE(LEFT(RIGHT(Id,10),5),"0","")&RIGHT(Id,5)&":ref"

NOTE: An important disclaimer here is that any formula you use for this purpose could one day begin failing if the format of the Case ID or Thread/Reference ID changes.

So in order to resolve for new cases, you should modify your custom formula if you prefer to use custom ref id formula or simply use standard Salesforce ref id.You can see that by simply sending an outbound email from a Case (using E2C from address) and observe that email with ref id in the recipient inbox.

So what really irritates me even more than their arrogant attitude about "it's not our problem, it's yours" is that they do not provide a viable method of using their "official" thread ID -- they do not provide access to that "field" via APEX or the page layout (which is the two places we need them).