How does email threading maintain conversation continuity across customer email replies?
Answer
Basics: Without threading, every reply would create a new case. When an agent sends an email from a case, Salesforce embeds identifiers so that when the customer replies, the inbound message can be matched to the same case, keeping the conversation in one Case Feed.
How it works: Setup > Email-to-Case (or Case Settings) > Threading options: insert the thread ID into the email subject and/or body, and enable email-header-based threading (recommended). The thread ID looks like ref:_00Dxxxxxxx._500xxxxxxx:ref, composed of a compressed org Id and case Id. On inbound mail, Salesforce checks headers first (if enabled), then the thread ID token, then creates a new case if no match is found. Matching replies are added as EmailMessage records on the case, and the case status may reopen or notify the owner depending on settings.
Advanced / deep dive: Header threading survives subject edits and removal of the token, so it is more reliable than token-only. The token depends on org Id, so after a sandbox refresh or org migration old tokens will not match; headers help. Apex lets you build the token for custom emails: EmailMessages.getFormattedThreadingToken(caseId) and decode with EmailMessages.getCaseIdFromEmailThreadId(token). When using external email sending (marketing or third-party service), pass the token or Reply-To routing address through so replies come back correctly. Merged cases and forwarded messages can behave unexpectedly, so test those flows. Spam and out-of-office replies thread into cases too, so filter them.
Code / config example:
// Add the threading token to a custom outbound email
Case c = [SELECT Id, CaseNumber FROM Case :caseId];
EmailMessages.getFormattedThreadingToken(c.Id);
Messaging. .SingleEmailMessage();
mail.setSubject( + c.CaseNumber);
mail.setPlainTextBody( + token);
mail.setToAddresses( <String>{ });
Messaging.sendEmail( <Messaging.Email>{ mail });