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.setTemplateId('00Xb0000000iwks');
mail.setTargetObjectId(a.CAccount__r.OwnerId); //lookup on account
mail.setToAddresses(new List<String>{a.CAccount__r.Owner.Email}); //email from account owner
mail.setTreatTargetObjectAsRecipient(false);
mail.setSaveAsActivity(false);
mail.setWhatId(a.Id);
this.mails.add(mail);
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 !?