---
schema_version: "1"
id: 51cf0e75447ad88c33e8329c78854d6363ae4cbe74e07df003432befd31de745
title: generateThreadingMessageId(caseId)
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_System_Cases_generateThreadingMessageId.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_System_Cases_generateThreadingMessageId.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 5e990753d8eba43219317458f5b68a0a6fb64d795934ee67bb31750478e0e8d9
description: Returns an RFC 2822-compliant message identifier that contains information used to match the email and its replies to a case.
---
# generateThreadingMessageId(caseId)

Returns an RFC 2822-compliant message identifier that contains information used to match the email and its replies to a case.

## Signature

public static String generateThreadingMessageId(Id caseId)

## Parameters

- **caseId**: Type: [Id](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_id.htm#apex_methods_system_id "Contains methods for the ID primitive data type.") The case SObject ID to which replies to this email should be attached.

## Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

## Usage

Use the returned message identifier when sending case-related emails in Apex. The returned message identifier can be used in Message-ID or References headers. However, because Salesforce doesn’t let users specify the Message-ID, we set this identifier in the References header. When users reply to the sent email, replies should be attached to the specified case.

## Example

In this sample, we create an email with a message identifier so that the email and any responses can be associated with the related case.

//Get your Case ID. Here we use a dummy ID
ID caseId = Id.valueOf('500xx000000bpkTAAQ');
//Create a SingleEmailMessage object
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
//Set recipients and other fields
email.setToAddresses(new String\[\] {'test@salesforce.com'});
email.setPlainTextBody('Test Email Notification');
//........... more fields ...........
//Get the threading message identifier
String messageId = Cases.generateThreadingMessageId(caseId);
//Insert the message identifier into the References header
email.setReferences(messageId);
//Send out the email
Messaging.sendEmail(new Messaging.SingleEmailMessage\[\]{email});