Do a quick Google search and you will quickly discover a number of formulas claiming to accurately generate a valid Salesforce Email-to-Case Thread Id. Unfortunately, the majority of these formulas are wrong (or at least make some incorrect assumption). The reality is that the Thread Id and and does change instance to instance. The only reliable method for retrieving a thread Id is to pull it from the email template logic itself. I.e. {!Case.Thread_id}.
Good news! This logic is available in a convenient apex action.
Via Flow
Introducing the Email-to-Flow Retrieve Salesforce Thread Id function. Simple pass in your case id(s) and return you thread id(s).
Note: One you Opt In to Disable REF ID and Transition to New Email Threading Behavior, the apex action will always return ‘’. This apex action should be used for transitional purposes only (as discussed in New Threading Behavior; Why Plan? (Part 1)).
Via Apex
//Initialize the apex action
List <emailtoflow.retrieveSalesforceCaseThreadId.parameter> params = new List <retrieveSalesforceCaseThreadId.parameter>();
//retrieve the case record which require a thread id populated
for(Case c : [select id
from case
where isClosed = false])
{
emailtoflow.retrieveSalesforceCaseThreadId.parameter param
= new emailtoflow.retrieveSalesforceCaseThreadId.parameter();
param.caseId = c.id;
params.add(param);
}
//Note: The nature of the native thread Id logic requires 1 SOQL Query per record
//Keep this in mind when running large batches of records
//Reminder: Once the new Threading behavior is activated, this no longer retrieves the thread id
//So, this must be executed well in advance
List <emailtoflow.retrieveSalesforceCaseThreadId.returnData> rd
= emailtoflow.retrieveSalesforceCaseThreadId.getThreadId(params);
for(emailtoflow.retrieveSalesforceCaseThreadId.returnData r : rd)
{
system.debug(‘—–Case Id: ‘ + r.caseId + ‘ ThreadId: ‘ + r.threadId);
//Implement you updating logic here
}