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…
Best Answer
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.