A quick Saturday morning code snippet from something I wrote this week.

Here’s how to generate an Email-To-Case thread ID for insertion into an email body or subject line.

You’ll need to pass in the id of the Case you want replies threaded to.

private String getThreadId(String caseId){
 return '[ ref:_' 
         + UserInfo.getOrganizationId().left(4) 
         + '0' 
         + UserInfo.getOrganizationId().mid(11,4) + '._' 
         + caseId.left(4) + '0' 
         + caseId.mid(10,5) + ':ref ]';
 }