function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

visualforce email template not resolving recipient and relatedTo at runtime (WhoID, WhatID)

My email template (below) works fine when using the "preview" function to populate the Recipient (WhoID) and RelatedTo (WhatID)  - but when a workflow rule triggers the emai lto be sent, it comes back blank as if it's not resolving the who or what correctly. .

<messaging:emailTemplate recipientType="Contact"
    subject="Your website Access"
    replyTo="" >

    <messaging:htmlEmailBody >        
        <p>Hello {!}--</p>
        <p>Here is a list of the website access you users currently have for account {!}:</p>
    <apex:datatable cellpadding="5" var="cts" value="{!relatedTo.Contacts}">
        <apex:column value="{!cts.Name}" headerValue="Name"/>
        <apex:column value="{!cts.Seat_Holder__c}" headerValue="Website Program Access"/>
        <apex:column value="{!cts.Email}" headerValue="Email" />
        <apex:column value="{!cts.Phone}" headerValue="Phone" />

I created a simple workflow rule that's called whenever a contact is edited. The workflow rule uses an email alert. The email Alert uses this template. I've checked to make sure the email template is for Contact objects, and that the Recipient is specified as Email Field: Email.

I must be missing something obvious..... any ideas?

Message Edited by icemft1976 on 10-24-2008 09:28 AM

Best Answer chosen by Admin (Salesforce Developers) 
Hi foks,  actually this should be resolved now.  Please let us know if you're finding otherwise.

All Answers

I have pinged the Messaging team to see if they can shed some light on this. Stay tuned.
Hi Doug,

thanks for your note.  I actually ended up writing the email routine as an apex class (I needed some conditional logic in choosing what related records would be displayed).

I had a heck of a time finding the Example for sending emails from APEX ( there's plenty of info on processing INbound emails with apex though!) - which is why i tried the visualforce tmeplates to begin with. But I'm glad I tried both ways.

Hopefully we'll get to use controllers with visualforce email templates in a future release. They're always working on improvements so I'll just promote it on the Idea Exchange and wait patiently :)

Thanks again!

ps: this issue is in our sandbox currently, the workflow rule, template and email alert names are all = Website New Account (liaison)

Same issue:- A Visualforce Email Template tests out ok in Preview mode.  It is also sent correctly from a record's Activity History Send An Email  button, when selecting the template.  The Visualforce Email Template does not, however, work when fired from within a workflow rule.  We tested a number of scenarios on this- various browsers, etc to no avail.  Perhaps there is additional info needed in the apex code to process via workflow?

The issue is that none of the data from the merge fields is populated when the Visualforce template is sent from a Workflow Rule Email Alert.  When sent manually or viewed in Preview mode, the Visualforce email template merge data populates correctly.

Our excitement about using the new Visualforce templates stems from a need to display related list items within the email template.

Thank you for any ideas/advice on this.


Message Edited by DPC3 on 10-30-2008 07:08 AM
This was also discovered internally by the AppStore team and a bug was opened.  The fix is now being verified and will deployed very soon. Visualforce email templates for workflow should operate pretty much as you expect, the "relatedTo" object will be the target of the approval process or the object being updated.  The "recipient" object will be the context user for email alerts and the approver for approval process emails.

Hoipe that helps.

Rick Stevens
Messaging Team Developer
Thanks Rick, that's great news!
We had a similar experience.

We used Visualforce email templates in an approval process for a new app. We did our first testing on October 15, and our last testing on October 17. Then we went into a quiet period for Dreamforce, and resumed our testing today, only to find the Visualforce templates were no longer working properly.

Any "relatedTo" merge field, e.g. {!relatedTo.Name}, now returns null. This used to work perfectly fine as of October 17.

We anxiously await a fix from SFDC :)

I had the same experience. I had posted on this board the same issue...

Is there a date when this is expected to be resolved?

Hi foks,  actually this should be resolved now.  Please let us know if you're finding otherwise.
This was selected as the best answer
Yep, I'll confirm that our Visualforce email template merge fields are working properly again - thanks!

I seem to be having this problem since first trying it in November. A Visualforce email template works well for preview and also when you use the "send email" button for a contact or other record. However, when the email is sent as an email alert from within a workflow rule, the merge fields are not merged in as they should be.


Logged as a bug back in November as Case# 02211213







Are none of the merge fields working for you, or just particular merge fields?  All the relatedTo merge fields appear to be defined correctly in your Visualforce Template, but I can see that you will have a problem on the {!} merge field when the template is used in a Workflow Email Alert.  When called from workflow, the recipientType will never be a Contact.  It will always be a user (the user that caused the workflow to be triggered).  When you use the template in Preview or the "Send email" button, you have the flexibility to select both a Contact (WhoId) and Safenet License (WhatId) you wish the email to be associated with.  In workflow, only the Safenet License that the workflow was triggered on is known for the relatedToType (WhatId).  If you have a contact name that you need to be included in the email, you can try building a relationship between your custom Safenet License object and a Contact, then use the related Contact merge field in your Visualforce Template.




Hi Chris,


OK, I seem to be getting somewhere, but have found another issue - that I originally thought was related.


Most merge fields are now being populated, I took your suggestion and used the contact that was related to the custom object (this relationship was already there), so this is now working. The only thing left now is that sections of the template with repeats such as:


<apex:repeat var="cx" value="{!relatedTo.LicenseCodes__r}">
        <h5><tt>{!cx.code__c }</tt></h5>      


Are not working when fired off from a workflow rule. They do work however when fired from within the UI.







I'm glad you have the contact name working now.


It appears that your workflow rule is setup to trigger on the creation of a new Safenet License (although I believe it's still possible to trigger this workflow if the last modified is changed on an existing license).  When you are testing the workflow trigger, is it on the creation of a new License?  I'm assuming when a new License is created, there are no codes associated to it yet.  If that is the case, nothing will get rendered for the snippet of markup you provided (the markup looks fine).  Or, are you somehow triggering the workflow via an update of a License that already has codes associated to it?  I setup something similar on my own org, and the related data is populated when the workflow is triggered on a update of an existing object that has related data associated to it.  Let me know if you confirm there are associated codes when workflow is triggered, and I will continue investigating.


Message Edited by ccopek on 05-13-2009 10:53 AM
Genius! I now have a secondary workflow that runs once the licence codes are inserted by our license server web service. Everything is working now, thanks.


Hi Folks,

Is this issue definitely resolved now, or could it have resurfaced? 

I've created a visualforce template for Contacts which works fine when I send it from a Send Email button on a Contact detail or Activity history screen, as mentioned above.


However, in a workflow rule based on Contact, and triggered simply by a change in a field on Contact, using the same template, it doesn't populate the ! field. It sends fine, just doesn't populate the field.


Any ideas?




When you set up your VF Template, you can specify two attributes in the <messaging:emailTemplate> top level tag:


recipientType - Can be Contact, Lead or User.  This is "who" the email is being sent to.

relatedToType - Can be any standard or custom entity.  This entity is often referred to as the "what" entity.  It is "what" the email is about or "related to".


When you send your email using the "Send Email" button from a Contact Detail, you are actually sending the email to the Contact.  The "who" of the email, which equates to the recipientType on the VF Template, is a Contact. 


When the VF Template is used in a Workflow Email Action, and triggered by a change on a Contact, the email will be sent to a User.  The "who" or recipient of the email is a User, not the Contact.  "What" the email is about, the "relatedTo", is the Contact.


In the case of your workflow template, if you want the name of the contact to be displayed, you need to make sure relatedToType is equal to "Contact", and change {!} to {!}. 


Hope that helps.




I have the same Issue with Custom HTML templates ( without using letterhead).


It works with "Send Email" button in Contact, and it looks fine in template preview, but if the email is sent via workflow,

althought the criteria matches, the mail is not sent. If I change the custom template with a "text template" it sends it correctly.


¿Anyone with the same problem?






If you can provide me with your Organization ID, Workflow Rule, Email Alert, and the ID or Name of the Custom HTML Template that is not working, I can take a look at your setup and try to determine what the problem is with your Custom HTML Template.


Chris Copek

Messaging Developer -


Thanks for the answer ccopek.


We already found the problem. The custom email template was incomplete because the plain-text version was empty.


It seems that salesforce willnot send the template if it does not have plain-text version.



This does not work for me. I always get 'sir/madam'. Its a VF email template.


<messaging:emailTemplate recipientType="Contact" 
subject="Notice of Closed Case & Client Survey: {!relatedto.CaseNumber}" >
<messaging:htmlEmailBody >
<style type="text/css">
body {font-family: Arial; size: 12pt;}

<body> <p> <apex:outputText value="Dear {!}," Rendered="{!IF(ISBLANK(, true, false)}" /> <apex:outputText value="Dear Sir/Madam," Rendered="{!IF(ISBLANK(, false, true)}" />





Based on what it looks like you are trying to do, I believe you have your boolean return values mixed up in the IF statements. Try this:


<!-- If the recipient's name is NOT blank, display it -->

<apex:outputText value="Dear {!}," Rendered="{!IF(ISBLANK(, false, true)}" />


<!-- If the recipient's name is blank, display Sir/Madam -->

<apex:outputText value="Dear Sir/Madam," Rendered="{!IF(ISBLANK(, true, false)}" />


The following will accomplish the same thing, but perhaps a little easier to read:


<!-- If the recipient's name is NOT blank, display it --> 

<apex:outputText value="Dear2 {!}," Rendered="{!NOT(ISBLANK(}" />


<!-- If the recipient's name is blank, display Sir/Madam -->

<apex:outputText value="Dear2 Sir/Madam," Rendered="{!ISBLANK(}" />







Thanks so much! 


2 other issues:


My colleague thinks that when a contact receives this email from an automated time based workflow, that the user will still look like the automated workkflow user, but the reply to email will be different, which will be confusing. Will that be the case? 


Also, is there anyway to get the case owners signature in this email?


Having problem sending vf emails from workflow,email sending with empty merge field values and id passing null to component/custom controller.Please need help on this.I want to resolve it soon.


Thanks in Advance.


Having problem sending vf emails from workflow,email sending with empty merge field values and id passing null to component/custom controller.Please need help on this.I want to resolve it soon.


 preview test email works as expected with all the data. 


Thanks in Advance.

Willem MulderWillem Mulder
We're still having this problem, and this completely breaks our interaction with our guests.

We are having several VF templates that are used in an email alert in a Process.

In short:
  • The templates merge fine when we test them manually (i.e. without triggering the Process, but do a test-and-verify)
  • The templates merge fine when we trigger the Process through the UI (i.e. we edit a record via the UI, the Process runs)
  • The templates DO NOT merge when we trigger the Process from Apex code (i.e. we edit the record via Apex)
Can anybody confirm this and/or share a solution? Thanks!