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
jdogsailingjdogsailing 

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?

Jeff

Best Answer chosen by Admin (Salesforce Developers) 

All Answers

jlojlo

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 >
{!relatedTo.Response__r.Account__r.OwnerId}
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

 

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:

http://wiki.apexdevnet.com/index.php/VisualForceEmailTemplates_sample

http://gokubi.com/archives/visualforce-email-templates

http://gokubi.com/archives/visual-force-email-templates-2

 

 

 

jdogsailingjdogsailing
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.
jdogsailingjdogsailing
Aha, you are using Visualforce email templates and I am using Text email templates. I will try it your way.
jdogsailingjdogsailing

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,

Jeff

 

 

<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}

Comments:
  {!relatedTo.Answer_Text__c}

</messaging:plainTextEmailBody>
</messaging:emailTemplate>

jlojlo
This was selected as the best answer