I have this RESTful controller enabled on my APEX Site to test email templates:

global class ActivationController {

    global static String email() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        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 >

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.


This looks like a known bug with relatedTo.{x} fields.

As a workaround, I've added a Contact object onto the relatedToType and used {!relatedTo.Contact__r.FirstName}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.