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
Manfred LinznerManfred Linzner 

Creating EmailMessage and associating with a Contact via API possible?

I'm working on a project where (already sent) emails should be saved to Salesforce and matched with the corresponding Salesforce contact.
Creating new Emails is rather straightforward. For example using the simple_salesforce python library my code looks like this:
from simple_salesforce import Salesforce


sf = Salesforce(instance_url=instance_url, session_id=session_id)
    {'FromAddress': '',
     'ToAddress': '',
     'Subject': 'Email: Fancy Subject', 
     'TextBody': 'lorem ipsum dolor sit amet',  
     'Status': 2

This successfully creates a new EmailMessage but the message is not matched to the contact (assuming is an exisiting contact in Salesfored). This can be seen in numerous places in the Salesforce UI. For example:
It is not part of the contact's activity history
When looking at the Email Message details, the section “Sender and Recipients” is empty
When creating new emails using the Salesforce UI, the association is done correctly. Comparing my EmailMessage objects from the ones generate by Salesforce there is one obvious difference: ActivityId is not set for my objects.
When I'm trying to set ActivityId to a matching Contact ID I receive a Malformed Request Exception INSUFFICIENT_ACCESS_OR_READONLY.

Am I missing something? Is it somehow possible to create those associations using the API?
Hi Manfred Linzner,

Did you find any solution for this. I'm also facing the same issue. How to connect Email Message to Contact?
Ty WhitfieldTy Whitfield
I've very late on this but stumbled across it while searching for something else.  According to SF, you can associate the EmailMessage object to a contact by populating the "BccIds" which the contact or lead ID.  Hope that helps