+ Start a Discussion

Making Email Template Merge Fields Work

According to the Help documents, it should be possible to follow relationship chains to fill out mail merge fields (e.g. "Opportunity.Account.CreatedBy.Phone"). For my custom objects; however, I can only get them to work across a single relationship.


So, "{!NullValue(Answer__c.Response__c.Account__c.OwnerFirstName, "Account Manager")}," seems like it ought to work but it doesn't. (In my object model, Answer__c has a lookup relationship named "Response" to a Response__c, which has a lookup relationship "Account" to an Account).


The string "Answer__c.Response__c" works, but the string "Answer__c.Response__c.Account__c" does not either.


I see there is some 40 character limit on merge field names? 40 characters, REALLY???


I've tried removing the "__c" names, also using "__r" but no help. Does this feature actually work? Can someone help get me back on the right track?


Best Answer chosen by Admin (Salesforce Developers) 

All Answers


What is the error message that you're seeing?


This code displays a value in my org:


<messaging:emailTemplate subject="Test" recipientType="Contact" relatedToType="Answer__c">
<messaging:plainTextEmailBody >


The component reference has some sample code:

https://na6.salesforce.com/apexpages/apexcomponents.apexp (click on the "usage" tab for the "messaging:emailTemplate" component)


Here are some more samples that might be useful:







I see no error messages at all, just blank fields in the emails. I'm using the web interface Administration Setup/Communication Templates/Email Templates to create my templates and I see no Visualforce xml anywhere. When I look for email templates in my Eclipse version, they aren't there and I see no way to get them downloaded. I'm glad it works for you because it means I can get there, but I'm still lost in space.
Aha, you are using Visualforce email templates and I am using Text email templates. I will try it your way.

I created a Visualforce email template as below and it almost works. The only thing I cannot do is access the Account Owner's first name in the salutation. I can get the OwnerId, but that is not useful. Everything I have tried to traverse to the User raises an exception. Where are these legal fields specified? The merge field dropdown in the text template editor is wrong, the Force.com Standard API fields don't work.


I tried using "recipient.FirstName" which does work; however, it seems to be using the Answer's owner instead of the Account owner that I specified in the Email Alert. The plumbing here is not working the way I want it to.


Still Frustrated,




<messaging:emailTemplate subject="Detractor Alert: Score={!relatedTo.Score__c}" recipientType="User" relatedToType="Answer__c">
<messaging:plainTextEmailBody >
Dear {!relatedTo.Response__r.Account__r.OwnerId}
This is an automated email about a new detractor incident. Please follow-up appropriately.

  Date: {!relatedTo.Date__c}
  Type: {!relatedTo.Question__r.Question_Type__c}
  Product: {!relatedTo.Question__r.Product__r.Name}
  Respondent: {!relatedTo.Response__r.Respondent__r.FirstName} {!relatedTo.Response__r.Respondent__r.LastName}
  Score: {!relatedTo.Score__c}



This was selected as the best answer