getRecordIdFromEmail(subject, textBody, htmlBody)
署名
public static Id getRecordIdFromEmail(String subject, String textBody, String htmlBody)
パラメーター
使用方法
メール-to-ケースで Lightning スレッドを有効にする必要があります。
件名と本文のいずれか、または両方にスレッドトークンが埋め込まれたメールが送信された場合、ほとんどのメールクライアントは、返信でメールの本文を引用し、件名を維持します。このメソッドは、返信に埋め込まれたスレッドトークンに一致した場合、対応するレコードを検出します。
通常、この方法は、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 // Parse thread-index header to remain consistent with Email-to-Case.
45 ClientThreadIdentifier = getClientThreadIdentifier(email.headers)
46 // Other fields you wish to add.
47 );
48
49 // Insert the new EmailMessage.
50 insert em;
51 System.debug('New EmailMessage Object: ' + em );
52
53 // Set the result to true. No need to send an email back to the user
54 // with an error message.
55 result.success = true;
56
57 // Return the result for the Apex Email Service.
58 return result;
59 }
60
61 private String getClientThreadIdentifier(List<Messaging.InboundEmail.Header> headers) {
62 if (headers == null || headers.size() == 0) return null;
63 try {
64 for (Messaging.InboundEmail.Header header : headers) {
65 if (header.name.equalsIgnoreCase('thread-index')) {
66 Blob threadIndex = EncodingUtil.base64Decode(header.value.trim());
67 return EncodingUtil.convertToHex(threadIndex).substring(0, 44).toUpperCase();
68 }
69 }
70 } catch (Exception e){
71 return null;
72 }
73 return null;
74 }
75}