0

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.

0

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.