getRecordIdFromEmail(subject, textBody, htmlBody)

Returns the record ID corresponding to the specified email threading token, or returns null if none is found.

public static Id getRecordIdFromEmail(String subject, String textBody, String htmlBody)

  • subject: Type: String The subject of the email.
  • textBody: Type: String The body of the email in text format.
  • htmlBody: Type: String The body of the email in HTML format.

Type: Id

The record ID that corresponds to the embedded threading token.

When you send emails with threading tokens embedded in the email subject, the email body, or in both the subject and body, most email clients quote the email body and maintain the email subject in a response. This method finds a corresponding record that matches the embedded threading token in a response.

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.