getFormattedThreadingToken(recordId)
Returns an email threading token that’s formatted with the correct prefix and suffix. This token can be embedded in an outbound email body, email subject, or both the body and subject. When users reply to the email, threading tokens can be used to attach responses to a record, such as a Case record in Email-to-Case.
public static Id getFormattedThreadingToken(Id recordId)
-
recordId:
Type:Id
The record ID associated with the threading token.
Type: String
The returned value is a formatted string that includes a prefix and suffix, for example:
When sending emails in Apex, use the returned string to match emails to a record, such as a Case record, that’s associated with the email thread. Embed the formatted token in the body or subject of outgoing emails. To find the corresponding record ID in incoming emails, use EmailMessages.getRecordIdFromEmail(subject, textBody, htmlBody).
If there is no existing token, getFormattedThreadingToken may perform a Data Manipulation Language (DML) operation to generate one.
If something goes wrong while generating the token, such as a user lacking permission to access the parent record, then null is returned.
In this sample, we send an email with a threading token so that the email and any responses are associated with the related case.