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?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityCase.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?
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.
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());
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');
"ref:_"&LEFT( $Organization.Id , 4)&"0"&RIGHT( $Organization.Id , 4)&"._"&LEFT(Id , 4)&"0"&RIGHT(Id , 5)&":ref"
that I sourced from success.salesforce.com/ideaView?id=087300000006trk
– cropredy
Dec 22 '15 at 18:30