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
Valentin MValentin M 

How are EmailMessage linked to the object from which they are sent ?

Hi all,

I'm working on some email integration stuff. I need to perform actions on the related object when an email is sent from Salesforce.

I understood that when you send an email from salesforce interface, it creates an EmailMessage record. So I defined a trigger on EmailMessage's "after insert" events but I can't figure out how EmailMessage records are linked to the SObject records they are related to. This link should exist since you can see the emails that were sent on the record's page but I already checked a few fields such as RelatedTo and ParentId fields, none of them contain the Id of the record they are associated to...

Thanks for your help
JLA.ovhJLA.ovh
Lookups for Emailmessage are :
ParentId, hosting the Case ID
ActivityId, hosting the Task Id
ReplyToEmailMessageId, referencing a previous email message
You should probably check the field value of ActivityId in your context
Samuel Hoffman 2Samuel Hoffman 2
This still seems to be an ongoing shortcoming of Salesforce.  Leads and Contacts are the most used objects for EmailMessages, so I don't understand why this wouldn't be in place.
J_TJ_T
As of API v.37, the EmailMessageRelation object can be used for contacts, leads, and users.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessagerelation.htm