I have this RESTful controller enabled on my APEX Site to test email templates:
@RestResource(urlMapping='/Activations/*')
global class ActivationController {
@HttpGet
@HttpPost
global static String email() {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00X3B000000HxnRUAS');
mail.setTargetObjectId('0033B00000BwGiiQAF');
mail.setWhatId('a033B000001P8vMQAS');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
I am trying to use this email template:
<messaging:emailTemplate subject="Subject" recipientType="Contact" relatedToType="Activation__c">
<messaging:htmlEmailBody >
<html>
<body>
{!recipient.FirstName}
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
When I run it, I get this error:
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] Class.ActivationController.email
If I delete {!recipient.FirstName}
from the template, I don't get the error anymore.
How can I show the contact's first name in the email? Is this a permissions thing? What am I doing wrong? Please note that the IDs I am using are absolutely valid. In addition, they reference the correct type of object they need to reference in order to populate SingleEmailMessage parameters correctly.