Find Case ThreadId using Apex and Formulas

Ensure that an email sent to your customer support email is attached to an existing case using Thread ID.


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



Apex -  ref:_'+ UserInfo.getOrganizationId().left(5) + UserInfo.getOrganizationId().mid(11,4) + '._'+ caseId.left(5)+ caseId.mid(10,5) + ':ref ]';


The actual extraction of the thread ID is looking for anything in between "ref:" and ":ref"


Example of a thread id: ref:_00D30oKPx._50030bwIii:ref


Note: If multiple thread IDs are included in the header or the body of the email sent, the email will attach to the Case that is represented by the first thread ID in the string:


Example  [ ref:_00D30oKPx._50030bwIii:ref ] [ ref:_00D30oKPx._50030bMuaN:ref ]  the email will attach to the case represented by ref:_00D30oKPx._50030bwIii:ref

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach