I am trying to figure out how to copy 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...

I set the following fields:

MailMessage: subject, textBody, toIds, ccIds, bccIds, toAddress, ccAddress, bccAddress, attachments

Task: whoId, subject, messageDate, status(completed), priority(normal), isReminderSet(false), isRecurrence(false), taskSubtype(Email), description(plain text representation of mail)

Any idea?

*ps: Other topics refer to the Case object, but the UI does not create a Case either...

  • 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. My money is on EmailMessageRelation table but I've never used it so I don't think it's worth being an answer.
    – eyescream
    Sep 21, 2017 at 22:21
  • Thanks for your reply. The EmailMessageRelation is used to relate the mail to a contact indeed, but is automaticly created and uses the toIds, ccIds and bccIds fields. It does not create or relate to an activity item.
    – Stijn
    Sep 22, 2017 at 8:39

1 Answer 1


I found one technique that might be useful. I'm creating the EmailMessage record (do not set the Status field), adding related EmailMessageRelation records (which can point to contact/lead), then updating the Status on the original EmailMessage record to Status = "3" (sent).

This causes salesforce to auto create the related Task record and associate it to the EmailMessage record and associated contacts/leads (using the EmailMessageRelation records). Still evaluating to see if there are any unknown side effects, but so far looks like a workable solution.

I tried your approach originally with no success.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .