getFormattedThreadingToken(recordId)
Signature
public static Id getFormattedThreadingToken(Id recordId)
Parameters
- recordId
- Type:Id
- The record ID associated with the threading token.
Return Value
Type: String
thread::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).
If there is no existing token, getFormattedThreadingToken may perform a Data Manipulation Language (DML) operation to generate one.
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.
// Get your Record ID. Here, we're using a dummy Case ID.
ID caseId = Id.valueOf('500xx000000bpkTAAQ');
// Get the formatted threading token.
String formattedToken = EmailMessages.getFormattedThreadingToken(caseId);
// Create a SingleEmailMessage object.
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Set recipients and other fields.
email.setToAddresses(new String[] {'test@example.com'});
// Append the threading token to the email body (text or html), subject,
// or both body and subject.
email.setPlainTextBody('Test Email Notification text body' + '\n\n' + formattedToken);
email.setHtmlBody('Test Email Notification html body' + '<br><br>' + formattedToken);
email.setSubject('Test Notification ' + '[ ' + formattedToken + ' ]');
// ........... more fields ...........
// Send out the email.
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});