EmailMessage not showing on lead's Activity Timeline

I am trying to log an email message on a lead/contact. The EmailMessage is getting logged correctly but it's not showing on the Activity Timeline for the lead/contact.

    EmailMessage m = new EmailMessage(
        Subject = 'email.subject',
        TextBody = 'email.plainTextBody',
        HtmlBody = 'email.htmlBody',
        ToIds = new Id[]{ leadContactId });
    insert m;

    EmailMessageRelation r = new EmailMessageRelation();
    r.emailMessageId = m.Id;
    r.relationId = leadContactId;
    r.relationType = 'FromAddress';
    insert r;

Here's the EmailMessage record showing a link to my sample lead record:

EmailMessage record

And here's the Activity Timeline on the lead record:

Activity Timeline on lead

Answers 1

  • You also need to update the status field on the EmailMessage object. Set it to 3 to indicate the message was sent.

    EmailMessage m = new EmailMessage(
    Subject = 'email.subject',
    TextBody = 'email.plainTextBody',
    HtmlBody = 'email.htmlBody',
    Status = '3',
    ToIds = new Id[]{ leadContactId });
    insert m;
    

    This automatically creates a TaskId and links the Task to the Email Message


Related Questions