Hello Everyone, In this tutorial, I am going to provide a code example that explain you, how to get thread id of email-to-case in apex. You can use this thread id in your email body to log customer email in single case id, when he replying on email. So, let’s begin,

Thread Id of Case

  ID orgId = UserInfo.getOrganizationId();
  String sOrgId = (string)orgId;
  ID caseId = caseId; \\ replace this with your case id
  String sCaseId = (string)caseId;
  String threadIdPart1 = 'ref:_' + sOrgId.left(5) + sOrgId.right(10).replace('0','');
  threadIdPart1 = threadIdPart1.substring(0,threadIdPart1.length()-3);
  String threadIdPart2 = '._' + sCaseId.Left(5) + sCaseId.right(10).left(5).replace('0','') + sCaseId.right(5);
  threadIdPart2 = threadIdPart2.substring(0,threadIdPart2.length()-3)+':ref';
  String threadId = threadIdPart1 + threadIdPart2;  
  System.debug('Thread Id^^'+threadId);

Output:

See also:

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.