getCaseIdFromEmailHeaders(headers)
Returns the case ID corresponding to the specified email header information, or returns null if none is found.
public static Id getCaseIdFromEmailHeaders(List<Messaging.InboundEmail.Header> headers)
- headers: Type: List<Messaging.InboundEmail.Header>
Type: Id
To optimize finding a match between email threads and cases in your custom code, we recommend that you use this method and EmailMessages.getRecordIdFromEmail to implement a combination of token- and header-based threading.
If you are transitioning from Ref ID threading, we recommend that you replace Cases.getCaseIdFromEmailThreadId with a combination of Cases.getCaseIdFromEmailHeaders and EmailMessages.getRecordIdFromEmail. If you choose to implement header-based threading only, replace Cases.getCaseIdFromEmailThreadId with Cases.getCaseIdFromEmailHeaders.
The headers argument is used to find the matching Case Id using values for the In-Reply-To and References headers based on RFC 2822. If Email-to-Case can’t find any emails with a matching In-Reply-To or References header, it also checks the incoming email for an Outlook-specific header called Thread-Index. The first 22 bytes of this header uniquely identify the thread. If Email-to-Case detects a Thread-Index header on the incoming mail, it looks for matching information in the ClientThreadIdentifier field in EmailMessage records. If a match is found, the customer’s reply email is linked to the related case.
Typically this method is used in Email Services so that you can provide your own handling of inbound emails using Apex code.
If you implement header-based threading in your Email Services currently, we recommend that you use Lightning threading, which combines token-based threading and header-based threading. For header-based threading to continue to work, store emails as EmailMessage records with the MessagedIdentifier field set properly. With Lightning threading, you can use threading tokens as the primary threading method and rely on header-based threading as a fallback, or vice versa.
In this example, we rely on threading tokens and use header-based threading as a fallback.