Salesforce API: Creating EmailMessage and associating a Contact possible?
Asked Answered
L

2

6

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)
sf.EmailMessage.create(
    {'FromAddress': '[email protected]',
     'ToAddress': '[email protected]',
     '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 [email protected] 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 I receive a Malformed Request Exception INSUFFICIENT_ACCESS_OR_READONLY.

Is it somehow possible to create those associations using the API?

Landgrabber answered 11/11, 2016 at 12:57 Comment(1)
Hy did you find some solution?Musil
L
1

The solution for me was:

a) While creating the email message object, pass ToIds, CcIds, BccIds instead of ToAddress, CcAddress, … when the contact exists in SF.

b) Unfortunately there is no FromIds field. So for the sender of the email message, this can be resolved by creating an EmailMessageRelation afterwards. E.g.:

sf.EmailMessageRelation.create({
                'EmailMessageId': email_message_id, # id of the email message created before
                'RelationAddress': from_contact_address, # sender email address
                'RelationId': from_relation_id, # SF Human object ID (in my case user id or contact id)
                'RelationType': 'FromAddress'
            })
Landgrabber answered 17/9, 2020 at 7:15 Comment(1)
May I ask how you set ToIds, CcIds and BccIds? I thought they should be Python strings but my request keeps failing...Ogdoad
E
0

Instead of that, you can set this mail as an attachment to the contact. Or best if you have a custom object storing the mails.

If you're planning to use an attachment, give parentId as contact Id.

If you're planning to create a custom object, set contact object as a lookup column(a relationship type in salesforce), which will allow you to set contact ID for the newly created column.

PS - EmailMessage is another type of object, it can't be stored.

Earshot answered 16/9, 2020 at 4:0 Comment(1)
Thanks! This works if you are the admin of the SF organization, but is unfortunately not a feasible solution for a customer integration.Landgrabber

© 2022 - 2024 — McMap. All rights reserved.