0

Case.Thread_Id

When is Thread_Id value generated?

I tried accessing it through SOQL and Apex and SFDC complains and throws error.

How do I access Thread_Id in apex?

Nilam Patel
  • 1
  • 1
  • 2

3 Answers3

5

Only reliable way to get the Thread Id at this time is when getting the result from Salesforce itself. You can do this by getting the result that Salesforce would generate when sending an Email:

// To be replaced with a real case Id.
Id caseId = '500c0000008kJL5'; 

List<Messaging.RenderEmailTemplateBodyResult> renderResults = Messaging.renderEmailTemplate(null, caseId, new List<String>{'{!Case.Thread_Id}'});

System.debug('this is the Case Thread Id: ' + renderResults[0].getMergedBody());
3

There have been several iterations of the thread ID, see this idea, but you shouldn't be using a regular regex, because that may be bad. Here's my suggestion:

public static String shortenID(String idValue) {
    String[] keys = idValue.left(15).split('');
    while(keys.size() > 5 && keys[5] == '0') {
        keys.remove(5);
    }
    return String.join(keys,'');
}

public String getThreadId(Id caseId) {
    return 
        'ref:_' + shortenId(UserInfo.getOrganizationId()) +
        ':_'    + shortenId(caseId) +  ':ref';
}

Anything less complex than this will work for the near future, but, as people have found out, the format does periodically change (the '_' characters are a "new" addition). What you should do, for now, is try send this ID simply by using actual standard templates instead of trying to build it yourself, because it will break one day.

Also... vote on the idea.

sfdcfox
  • 489,769
  • 21
  • 458
  • 806
1

Case Thread ID is the combination of Organization Id and Case ID so You can create a formula field on Case object with following value as per the Salesforce document

"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"

in apex you can use following code

public static String CASE_REF_FORMAT = 'ref:{0}.{1}:ref';

@testVisible
private static String shortenOrgId(String id) {
     String part = id.substring(0,15);
     Pattern p = Pattern.compile('^([A-Za-z0-9]{5})([A-Za-z0-9]*)$');
     Matcher m = p.matcher(part);

     if (m.matches()) {
          return '_' + m.group(1) + m.group(2).replace('0', '');
     }

     return '';
}

@testVisible
private static String shortenCaseId(String id) {
     String part = id.substring(0,15);
     Pattern p = Pattern.compile('^([A-Za-z0-9]{5})([A-Za-z0-9]*)([A-Za-z0-9]{5})$');
     Matcher m = p.matcher(part);

     if (m.matches()) {
          return '_' + m.group(1) + m.group(2).replace('0', '') + m.group(3);
     }

     return '';
}

public static String caseRefId(String orgId, String caseId) {
     if (orgId == null || caseId == null) {
          return '';
     }

     String shortenedOrgId = shortenOrgId(orgId);
     String shortenedCaseId = shortenCaseId(caseId);

     return String.format(
          CASE_REF_FORMAT,
          new List<String>{
               shortenedOrgId,
               shortenedCaseId
          }
     );
}

example :

caseRefId('YOUR_ORG_ID','YOUR CASEID');
Himanshu
  • 10,486
  • 5
  • 20
  • 33
  • Both the formula and Apex Code are technically incorrect. You're close on both counts, though. For example, the hypothetical case ID 5003500307ARiL1 would be rendered as _50035:_37ARiL1 instead of _50035:_307ARiL1. – sfdcfox Dec 22 '15 at 18:22
  • Interesting, I used "ref:_"&LEFT( $Organization.Id , 4)&"0"&RIGHT( $Organization.Id , 4)&"._"&LEFT(Id , 4)&"0"&RIGHT(Id , 5)&":ref" that I sourced from https://success.salesforce.com/ideaView?id=087300000006trk – cropredy Dec 22 '15 at 18:30
  • yah i got that from that idea only – Himanshu Dec 22 '15 at 18:31
  • Thank you! So basically Thread_Id could be a hidden field on the case correct? – Nilam Patel Dec 22 '15 at 18:33
  • 1
    @crop1645 That version is technically incorrect, as I recently found out that the 4-5 positions on the ID are pod identifiers, but has historically been a 0 (because they hadn't counted that high yet). So, LEFT(Id, 5) is the minimum safe value, then you need to simply delete zeroes until you come to the first non-zero value. While today, we know there are five significant characters in most IDs, that won't be always be true in the future. – sfdcfox Dec 22 '15 at 18:43
  • @sfdcfox Wow -- I guess I would have figured it out if my org's pod fell into the more recent ones. This deserves a new self-answered OP on SFSE that I'm sure you'd get lots of upvotes for (not that you need any :-) ) – cropredy Dec 22 '15 at 18:48
  • No, actually, there's a wonderful little piece on the anatomy of an ID already: http://salesforce.stackexchange.com/questions/1653. It's notably more complicated than it appears at first blush. – sfdcfox Dec 22 '15 at 18:50