Newer Version Available
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.
Signature
public static Id getFormattedThreadingToken(Id recordId)
Parameters
- recordId
- Type:Id
- The record ID associated with the threading token.
Return Value
Type: String
The returned value is a formatted string that includes a prefix and suffix, for
example:
1thread::pp5XPGfmNf2hRZdRCWnrohc::Usage
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).
Example
In this sample, we send an email with a threading token so that the email and any responses are associated with the related case.
1// Get your Record ID. Here, we're using a dummy Case ID.
2ID caseId = Id.valueOf('500xx000000bpkTAAQ');
3
4// Get the formatted threading token.
5String formattedToken = EmailMessages.getFormattedThreadingToken(caseId);
6
7// Create a SingleEmailMessage object.
8Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
9
10// Set recipients and other fields.
11email.setToAddresses(new String[] {'test@example.com'});
12
13// Append the threading token to the email body (text or html), subject,
14// or both body and subject.
15email.setPlainTextBody('Test Email Notification text body' + '\n\n' + formattedToken);
16email.setHtmlBody('Test Email Notification html body' + '<br><br>' + formattedToken);
17email.setSubject('Test Notification ' + '[ ' + formattedToken + ' ]');
18// ........... more fields ...........
19
20// Send out the email.
21Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});