+ Start a Discussion
dmchengdmcheng 

Email template merge fields not populating when sent via Apex

I'm having trouble with email template merge fields that are not being populated when the email is sent by Apex.

I have an Apex method that uses setTemplateId, setWhatId, and SetTargetObjectId. The template is an HTML template, not Visualforce. setTargetObjectId is set to a contact. setWhatId is a custom object that has no relationships with the contact object.

In the email template, I want to merge {!Contact.Name}. When the Apex method runs, it sends the email and links the activity to the contact, but the contact name is always blank in the email body.

Is this because the merge fields must be in the WhatId custom object? Or because the contact is linked to a portal user?