# How does email threading maintain conversation continuity across customer email replies?

**Topic:** Service Cloud · **Difficulty:** Medium · **Tags:** Service Cloud, Email Threading, Case Conversations

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
```apex
// Add the threading token to a custom outbound email
Case c = [SELECT Id, CaseNumber FROM Case WHERE Id = :caseId];
String token = EmailMessages.getFormattedThreadingToken(c.Id);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Update on case ' + c.CaseNumber);
mail.setPlainTextBody('We have an update.\n\n' + token);
mail.setToAddresses(new List<String>{ 'customer@example.com' });
Messaging.sendEmail(new List<Messaging.Email>{ mail });
```

**Hidden gotchas & must-remember points:**
- Sandbox refresh or org copy changes the org part of the token, breaking old threads.
- If the customer deletes the token and header threading is off, a new case is created.
- Both token and header threading can be enabled together; header threading is the safer choice.
- Replies to closed cases may reopen them or create new cases depending on configuration.
- Email clients that rewrite subjects can drop the token.
- The Reply-To must be the routing address, or replies never reach Salesforce.

**Best practice:** Enable both header and thread ID threading, and include the token in every system-generated message about a case.

---
Source: ForceChallenges · last updated 2026-11-02
