+1 vote
1 view
in Salesforce by (11.3k points)

Im trying to send an email in Apex with the SingleEmailMessage() function using an existing Template and connecting it with a custom object record.

mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(a.CAccount__r.OwnerId);  //lookup on account
mail.setToAddresses(new List<String>{a.CAccount__r.Owner.Email}); //email from account owner

Here i want to fill the template data with the custom object record "a". But i get the following error:

WhatId is not available for sending emails to UserIds.

Nowhere could i find an explicit answer that an Email in Apex can only be sent with a contact object in setTargetObjectId(). What i want to refrain from doing is to temporarily create a contact for the sole purpose of sending an email !?

Thanks in advance if someone has an idea

Please log in or register to answer this question.