Newer Version Available

This content describes an older version of this product. View Latest

getCaseIdFromEmailHeaders(headers)

Returns the case ID corresponding to the specified email header information, or returns null if none is found.

Signature

public static Id getCaseIdFromEmailHeaders(List<Messaging.InboundEmail.Header> headers)

Parameters

headers
Type: List<Messaging.InboundEmail.Header>

Return Value

Type: Id

Usage

The headers argument is used to find the matching Case Id using values for the In-Reply-To and References headers based on RFC 2822. To determine the Case Id, Email-to-Case also uses certain third-party email client-specific identifiers in addition to identifiers in standard headers.

Typically this method is used in Email Services so that you can provide your own handling of inbound emails using Apex code.

Example

For threading to work properly, you must store inbound emails as EmailMessage objects. In the following example, we try to find an existing Case record using Cases.getCaseIdFromEmailHeaders(). If the method returns null, no existing Case record was found and we create a new case.

1global class AttachEmailMessageToCaseExample implements Messaging.InboundEmailHandler {
2    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
3                                                        Messaging.InboundEnvelope env) {
4 
5        // Create an InboundEmailResult object for returning the result of the 
6        // Apex Email Service
7        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
8    
9        // Try to find a Case based on the email headers
10        Id caseId = Cases.getCaseIdFromEmailHeaders(email.headers);
11
12        // If no Case found, create a New Case.
13        if (caseId == null) {
14            Case c = new Case(Subject = email.subject);
15            insert c;
16            System.debug('New Case Object: ' + c);
17            caseId = c.Id;
18        }
19
20        // Process recipients
21        String toAddresses;
22        if (email.toAddresses != null) {
23            toAddresses = String.join(email.toAddresses, '; ');
24        }
25
26        // To store an EmailMessage for threading, you need at minimum
27        // the Status, the MessageIdentifier, and the ParentId fields
28        EmailMessage em = new EmailMessage(
29            Status = '0',
30            MessageIdentifier = email.messageId,
31            ParentId = caseId,
32            // Important fields
33            FromAddress = email.fromAddress,
34            FromName = email.fromName,
35            ToAddress = toAddresses,
36            TextBody = email.plainTextBody,
37            Subject = email.subject
38            // Other fields you wish to add
39        );
40        
41        // Insert the new EmailMessage 
42        insert em;
43        System.debug('New EmailMessage Object: ' + em );   
44    
45    // Set the result to true. No need to send an email back to the user 
46    // with an error message
47    result.success = true;
48    
49    // Return the result for the Apex Email Service
50    return result;
51  }
52}