Newer Version Available
getCaseIdFromEmailHeaders(headers)
署名
public static Id getCaseIdFromEmailHeaders(List<Messaging.InboundEmail.Header> headers)
パラメーター
- headers
- 型: List<Messaging.InboundEmail.Header>
戻り値
型: Id
使用方法
カスタムコード内のメールスレッドおよびケース間で一致項目がないかを検索する動作を最適化するために、このメソッドと EmailMessages.getRecordIdFromEmail を使用して、トークンベーススレッドとヘッダーベーススレッドを組み合わせて実装することをお勧めします。
参照 ID スレッドから移行する場合は、Cases.getCaseIdFromEmailThreadId を Cases.getCaseIdFromEmailHeaders と EmailMessages.getRecordIdFromEmail の組み合わせに置き換えることをお勧めします。ヘッダーベーススレッドのみを実装する場合は、Cases.getCaseIdFromEmailThreadId を Cases.getCaseIdFromEmailHeaders に置き換えます。
headers 引数は、RFC 2822 に基づいて In-Reply-To ヘッダーと References ヘッダーの値を使用して一致するケース ID を見つけるために使用されます。メール-to-ケースで In-Reply-To または References ヘッダーが一致するメールが見つからない場合は、受信メールで Thread-Index と呼ばれる Outlook 固有のヘッダーもチェックされます。このヘッダーの最初の 22 バイトでスレッドが一意に識別されます。メール-to-ケースで受信メールの Thread-Index ヘッダーが検出された場合、EmailMessage レコードの ClientThreadIdentifier 項目で一致する情報が検索されます。一致が見つかった場合、顧客の返信メールが関連ケースにリンクされます。
通常、この方法は、Apex コードを使用して受信メールを独自に処理できるようにするためにメールサービスで使用されます。
例
現在メールサービスでヘッダーベーススレッドを実装している場合は、トークンベーススレッドとヘッダーベーススレッドを組み合わせた Lightning スレッドを使用することをお勧めします。ヘッダーベーススレッドで作業を続行するには、MessagedIdentifier 項目が正しく設定された EmailMessage レコードとしてメールを保存します。Lightning スレッドでは、スレッドトークンを主要なスレッド方式として使用し、ヘッダーベーススレッドをフォールバックとして利用することも、その逆を行うこともできます。
次の例では、スレッドトークンを利用して、ヘッダーベーススレッドをフォールバックとして使用しています。
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 the Case ID using threading tokens in email attributes.
10 Id caseId = EmailMessages.getRecordIdFromEmail(email.subject, email.plainTextBody, email.htmlBody);
11
12 // If we haven't found the Case ID, try finding it using headers.
13 if (caseId == null) {
14 caseId = Cases.getCaseIdFromEmailHeaders(email.headers);
15 }
16
17 // If a Case isn’t found, create a new Case record.
18 if (caseId == null) {
19 Case c = new Case(Subject = email.subject);
20 insert c;
21 System.debug('New Case Object: ' + c);
22 caseId = c.Id;
23 }
24
25 // Process recipients
26 String toAddresses;
27 if (email.toAddresses != null) {
28 toAddresses = String.join(email.toAddresses, '; ');
29 }
30
31 // To store an EmailMessage for threading, you need at minimum
32 // the Status, the MessageIdentifier, and the ParentId fields.
33 EmailMessage em = new EmailMessage(
34 Status = '0',
35 MessageIdentifier = email.messageId,
36 ParentId = caseId,
37 // Other important fields.
38 FromAddress = email.fromAddress,
39 FromName = email.fromName,
40 ToAddress = toAddresses,
41 TextBody = email.plainTextBody,
42 HtmlBody = email.htmlBody,
43 Subject = email.subject
44 // Other fields you wish to add.
45 );
46
47 // Insert the new EmailMessage.
48 insert em;
49 System.debug('New EmailMessage Object: ' + em );
50
51 // Set the result to true. No need to send an email back to the user
52 // with an error message.
53 result.success = true;
54
55 // Return the result for the Apex Email Service.
56 return result;
57 }
58}