I am creating a new EmailMessage record from my apex code and I want that this record should be associated to a Lead record. As per documentation, we can not use RelatedToId

RelatedToId : The RelatedToId represents nonhuman objects such as accounts, opportunities, campaigns, cases, or custom objects. RelatedToIds are polymorphic. Polymorphic means a RelatedToId is equivalent to the ID of a related object.

and if I try to use it , I get a


Is there any way we can associate EmailMessage to Lead?

The larger picture about what I have to do here is that:

We are sending an single email message from a lightning component button click through apex SingleMessage. When we do this, a record of activity history should be created for this lead and I have to add some custom fields on this activity history record to show some more information. I used setWhatId and setSaveAsActivity(true) methods of Messaging.SingleEmailMessage class but that gives a similar error as RelatedToId:

SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, SaveAsActivity is not allowed with whatId that is not supported as a task whatId.: [whatId, 00Q3a00000pHXf4]

1 Answer 1


Instead of setWhatId, which sets the What Id (thing) for the activity, use setTargetObjectId(targetObjectId), which sets the Who Id. Then you can set save activity, etc.

  • This will create an activity history record related to Lead alright. But I want to update some custom fields on this activity history record created for Lead. How can I achieve that? I can create a separate question for that if needed.
    – Sarang
    May 17, 2021 at 10:18
  • As this question is closed, added a new questions with more details: salesforce.stackexchange.com/questions/343454/…
    – Sarang
    May 18, 2021 at 4:27

You must log in to answer this question.

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