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
Stijn VermeulenStijn Vermeulen 

Relate EmailMessage to activity history, EmailMessage.AcitivityId is not writable

I am trying to figure out how to migrate emails + attachments from one system to SF using a java application.

When creating an email from the Salesforce Lightning UI, an EmailMessage and a Task is created. This way it shows up in the Activity timeline of the contact and by clicking the link you get redirected to the email.

I managed to do the same, create an EmailMessage and a Task, but the task is not related to the mail message. When clicking the link in the activity, I go to the Task object and not the MailMessage. It also doesn't display as nicely. It makes sence because I don't relate the task id to the mail message, because the EmailMessage.AcitivityId is not writable.

How to create an email, relate it to a contact and make it show up in the activity feed?

I am using lightning btw.
NagendraNagendra (Salesforce Developers) 
Hi Vermeulen,

Your best bet might be to query the Contact for which you know it worked OK and all his related lists. Maybe try from Force.com IDE, it has a fairly easy relationship explorer.

May I suggest you please give a try using EmailMessageRelation https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessagerelation.htm table which might help you further with the above requirement.

Hope this helps.

Thanks,
Nagendra
Stijn VermeulenStijn Vermeulen
Hi Nagendra,

The EmailMessageRelation object is generated automaticly and if I compare a working example with the one created by my code they contain the same data. I am pretty sure the relationship depends on the activityId, which is read only. I have tried several ways of contacting Salesforce with my question, even direct contact with an Salesforce employee/developer, but no one seems to be able to answer my question. My conclusion is that the EmailMessage object is for internal use only and was never meant to be extended or used by the community.

Thanks,
Stijn
Kaushik Muhuri 9Kaushik Muhuri 9
The EmailMessageRelation object is to be used in conjunction with EmailMessage and the Lead/Contact/.....as follows:
        EmailMessageRelation emr = new EmailMessageRelation();
        emr.RelationAddress = fromAddress;
        emr.RelationType = 'FromAddress';
        emr.EmailMessageId = emLd.Id;
        emr.RelationId = ld.Id;

Here, ld is the Lead to which the EmailMessage has to be tagged and emLd is the EmailMessage object created.
Hope this answers your query.