Newer Version Available
getCaseIdFromEmailHeaders(headers)
署名
public static Id getCaseIdFromEmailHeaders(List<Messaging.InboundEmail.Header> headers)
パラメータ
- headers
- 型: List<Messaging.InboundEmail.Header>
戻り値
型: Id
使用方法
headers 引数は、RFC 2822 に基づいて In-Reply-To ヘッダーと References ヘッダーの値を使用して一致するケース ID を見つけるために使用されます。ケース ID を特定するため、メール-to-ケースでは、標準ヘッダーの識別子に加えて、特定のサードパーティのメールクライアント固有の識別子も使用されます。
通常、この方法は、Apex コードを使用して受信メールを独自に処理できるようにするためにメールサービスで使用されます。
例
スレッドが正常に機能するためには、受信メールを EmailMessage オブジェクトとして保存する必要があります。次の例では、Cases.getCaseIdFromEmailHeaders() を使用して既存のケースレコードを検索することを試みます。このメソッドで null が返された場合、既存のケースレコードは見つかっておらず、新しいケースが作成されます。
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}