function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AndegosuAndegosu 

How to set reference of EmailMessage to Case using setReferences()?

With the new update of email-to-case threading behavior, Thread-Id field is being deprecated: https://releasenotes.docs.salesforce.com/en-us/winter21/release-notes/rn_email_to_case.htm 

I have an apex class that sends out an auto-response to contacts in a case based on a integration to a customer portal.

I added the thread-id before from the case before but now when this doesn't work I need to set header info so when a contact responds to the auto-response the response is added to the case, and doesn't create a new case according to this new email-to-case behavior.

I have tried using the setReferences() on Messaging.SingleEmailMessage to set a reference to the case, but I can't find proper documentation of how to set the reference, I just need a reference to the case so the response gets inserted correctly.
AnudeepAnudeep (Salesforce Developers) 
My understanding of the setReferences() method is the SingleEmailMessage class should ideally be populated with the parent emails' References and message IDs to identify a thread

You will need to write some code to generate the unique thread reference and pass the value into the method

If you try using ef:_00D0EDm2o._5000E978zj:ref you might get the error INVALID_MESSAGE_ID_REFERENCE, References header is not valid.

You will see this error because the reference should contain the parent emails' References and message IDs, and possibly the In-Reply-To fields. It has to be in proper format fpr example <19951223192543.3034.qmail@silverton.berkeley.edu> ( <> is not necessary to add to message id)

References can contain multiple Message-ID with space between them.

A Message-ID is like this: <19951223192543.3034.qmail@silverton.berkeley.edu>
Mail address encoded between <>

Reference: <19951223192543.3034.qmail@silverton.berkeley.edu> <1995134343492543.3034.qmail@silverton.berkeley.edu>

The below document can be useful to understand this:

https://cr.yp.to/immhf/thread.html
AndegosuAndegosu
Thank you for your answer Anudeep, I will look into to try and generate a unique Id. I tried using email headers from earlier messages which worked fine but the problem is for cases without existing emails with headers.
Kostiantyn HladkyiKostiantyn Hladkyi
Why SalesForce documentation doesn't contain any information/example about it?
SingleEmailMessage Methods | Apex Reference Guide | Salesforce Developers
Etienne Coutant 8Etienne Coutant 8
Hi,
Were you able to figure out how to set the Message-ID when sending outbound emails with Apex?
AndegosuAndegosu
Hi, by setting emailMsg.whatId(caseId) and emailMsg.setSaveAsActivty(false) was enough in my use case to get the reference right on the first email sent out without adding the email to the case. 
Scott Fenech 7Scott Fenech 7
@Andegosu

Did that work for you? 
Yogaraj Kandasamy VadivelYogaraj Kandasamy Vadivel

@Andegosu, Thanks for the solution. Its working fine in my usecase where I already have an email message associated to the case and I am sending the email through apex for the subsequent emaill. Now user can reply to the email notification which is successfully adding back to the parent case instead of new case. 

Your solution to add the email headers from earlier messages is working as expected. 
 

Yadhu Krishnan SYadhu Krishnan S
@Andegosu, Yes it worked for me too, but I didn't have to keep setSaveAsActivty to False.