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
Ido Greenbaum 10Ido Greenbaum 10 

VisualForce Email Template as part of a WorkFlow- merge fields not populating

Hi Community, 

I have the below VisualForce Email Template:
 
<messaging:emailTemplate subject="[Email] #{!relatedTo.CaseNumber} - {!relatedTo.Subject} - {!relatedTo.Contact.Name}" recipientType="User" relatedToType="Case">
<messaging:htmlEmailBody >
<html>
<style type="text/css">
body {font-family: arial; size: 12pt;}
</style>
<body>
    <img src="https://my.salesforce.com/servlet/servlet.ImageServer?id=01561000001JYOm&oid=OBFUDCATED"/><br/>
A <b>new Email</b> was added to the case below:
<br />------------------------
<br />• Case #: {!relatedTo.CaseNumber}
<br />• Account:&nbsp;<apex:outputLink value="https://my.salesforce.com/{!relatedTo.Account}">{!relatedTo.Account.Name}</apex:outputLink>
<br />• Contact Name:&nbsp;<apex:outputLink value="https://my.salesforce.com/{!relatedTo.Contact}">{!relatedTo.Contact.Name}</apex:outputLink>
<br />• Case status: {!relatedTo.Status}
<br />• Priority: {!relatedTo.Priority}<apex:image id="theImage" value="{!relatedTo.Priority_Flags_URL__c}" width="12" height="12"/>
<br />• Link: https://my.salesforce.com/{!relatedTo.Id}
<apex:outputPanel rendered="{!relatedTo.Jira__c!=null}" >
<br />• Jira issue:&nbsp;<apex:outputLink value="https://www.atlassian.net/browse/{!relatedTo.Jira__c}">{!relatedTo.Jira__c}</apex:outputLink></apex:outputPanel>
<apex:outputPanel rendered="{!relatedTo.Git_Link__c!=null}" >
<br />• Git Link:&nbsp;<apex:outputLink value="{!relatedTo.Git_Link__c}">{!relatedTo.Git_Link__c}</apex:outputLink></apex:outputPanel>
<br />------------------------
<apex:outputPanel rendered="{!relatedTo.Last_Incoming_Email_Content__c!=null}" style="white-space:pre;">
<h3><br /><b><u>Case Last Incoming Email:</u></b></h3><br />
<apex:outputField value="{!relatedTo.Last_Incoming_Email_Content__c}" style="white-space:pre;"></apex:outputField>
</apex:outputPanel>
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
This VF Email is used in a WorkFlow rule, to send and Email Alert for each new incoming Email to the case which is under his custody. The WorkFlow Rule is pretty simple, it 'listens' to the change in the Custom Field 'Last_Incoming_Email_Content__c', and fires the email notification.

The issue is that the Email notification doesn't populate the Merge Fields: 

User-added image

Below is my email client's 'original' massage:
Date: Mon, 31 Oct 2016 12:13:08 +0000 (GMT)
From: System <obfuscated>
Sender: noreply@salesforce.com
To: "obfuscated" <obfuscated>
Message-ID: <QLFAw000000000000000000000000000000000000000000000OFWV9W004MUKYztRQrCfzfyDk0URBQ@sfdc.net>
Subject: [Email] #00008640 -
  -
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="----=_Part_16014_1675508803.1477915988038"
X-SFDC-LK: 00D61000000edsH
X-SFDC-User: 00561000001nhq1
X-Sender: postmaster@salesforce.com
X-mail_abuse_inquiries: http://www.salesforce.com/company/abuse.jsp
X-SFDC-TLS-NoRelay: 1
X-SFDC-Binding: 1WrIRBV94myi25uB
X-SFDC-EmailCategory: workflowActionAlert
X-SFDC-EntityId: obfuscated
X-SFDC-Interface: internal

------=_Part_16014_1675508803.1477915988038
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit


------=_Part_16014_1675508803.1477915988038
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit


<html>
<style type="text/css">
<!--

body {font-family: arial; size: 12pt;}

//-->
</style>
In addition, when testing with the 'test and verify merge fiels' in the Email Template editor, everything looks fine:

User-added image

Thank you
LakshmanLakshman
Try this for fetching Account and contact name-
<br />• Account:&nbsp;<apex:outputLink value="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4)+relatedTo.AccountId}">{!relatedTo.Account.Name}</apex:outputLink>

<br />• Contact Name:&nbsp;<apex:outputLink value="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4)+relatedTo.ContactId}">{!relatedTo.Contact.Name}</apex:outputLink>

Also you should avoid hardcoding salesforce URL's in email templates as it changes in Sandbox instances. The above solution should work in all of your environments.

Mark it as Best Answer if it helps.
Ido Greenbaum 10Ido Greenbaum 10
Thank you @Lakshman. 
I will avoid hardcoding links to allow sandbox support. 

In regards the original issue - I changed the links and they had no affect. Issue presist, as the Email Message still doesn't populate the merge fields. 
 
LakshmanLakshman
It should be working as field level security doesnt matter as its fired via workflow. Can you check the email source and see if account id is populated for the a href. 
Ido Greenbaum 10Ido Greenbaum 10
It seems that the Account  href wasn't populated: 
Account:&nbsp;<a href="https://*.salesforce.com/"></a>

Below is the full 'original' message from my Email Client (Gmail): 
User-added image
LakshmanLakshman
Are you sending this email to Salesforce User or Standard platform user. Does the user have access to account but this should not matter, worth checking though.
Ido Greenbaum 10Ido Greenbaum 10
The 'recipientType' in the VisualForce Email Template is declated 'User':
<messaging:emailTemplate subject="[Email] #{!relatedTo.CaseNumber} - {!relatedTo.Subject} - {!relatedTo.Contact.Name}" recipientType="User" relatedToType="Case">
The User, which is my System Administrator profile have access to the Case fields, so I'm guessing this is not the cause. 
I changed the recipientType="Contact" and tested, with same results. 
 
LakshmanLakshman
Is the case record which you are testing having account and contact filled in ? 
If yes, you might have to set debug logs before sending email and see whats happening. 
Ido Greenbaum 10Ido Greenbaum 10
Thanks for the ongoing support Lakshman. 
The test case has an associated account, and contact. These Merge Fields appear properly when using the 'test and verify merge fiels' in the VisualForce Email Editor. Furthermore, there are other Merge Fields which are not populating in the Email Client, such as my custom 'Last_Incoming_Email_Content__c'. 
The interesting thing is that some Merge Fields do work. The CaseNumber is a goo eample for a Merge Files which populated in the HTML body, but wasn't able to populate in the Email Subject. 

I turned on the finest debugging using Debug Logs, and captured the logs: 
 
​Log38.0 APEX_CODE,FINEST;APEX_PROFILING,FINEST;CALLOUT,FINEST;DB,FINEST;SYSTEM,FINE;VALIDATION,INFO;VISUALFORCE,FINER;WORKFLOW,FINER 
14:00:54.0 (531645)|USER_INFO|[EXTERNAL]|00561000000dSc6|test@test.com|Standard Time|GMT+02:00 
14:00:54.0 (566377)|EXECUTION_STARTED 
14:00:54.0 (570517)|CODE_UNIT_STARTED|[EXTERNAL]|VisualForce View State 
14:00:54.0 (13906157)|CODE_UNIT_FINISHED|VisualForce View State 
14:00:54.0 (15083270)|EXECUTION_FINISHED 14:00:54.21 (21190170)|CUMULATIVE_PROFILING_BEGIN 14:00:54.21 (21190170)|CUMULATIVE_PROFILING|No profiling information for SOQL operations 
14:00:54.21 (21190170)|CUMULATIVE_PROFILING|No profiling information for SOSL operations 
14:00:54.21 (21190170)|CUMULATIVE_PROFILING|No profiling information for DML operations 
14:00:54.21 (21190170)|CUMULATIVE_PROFILING|No profiling information for method invocations 
14:00:54.21 (21190170)|CUMULATIVE_PROFILING_END
Unfurtunately, the logs doesn't seem to have enough information to further debug this. 


 
LakshmanLakshman
No problem Ido, its my pleasure and learning in helping.

I tried the same template in my developer org and it seems its working for me
VF Template-
<messaging:emailTemplate subject="[Email] #{!relatedTo.CaseNumber} - {!relatedTo.Subject} - {!relatedTo.Contact.Name}" recipientType="User" relatedToType="Case">
<messaging:htmlEmailBody >
<html>
<style type="text/css">
body {font-family: arial; size: 12pt;}
</style>
<body>
    <img src="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4) & '/servlet/servlet.ImageServer?id=01561000001JYOm&oid=xxxxxxxx'}"/><br/>
A <b>new Email</b> was added to the case below:
<br />------------------------
<br />• Case #: {!relatedTo.CaseNumber}
<br />• Account:&nbsp;<apex:outputLink value="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4) + relatedTo.AccountId}">{!relatedTo.Account.Name}</apex:outputLink>
<br />• Contact Name:&nbsp;<apex:outputLink value="{!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4) + relatedTo.ContactId}">{!relatedTo.Contact.Name}</apex:outputLink>
<br />• Case status: {!relatedTo.Status}
<br />• Priority: {!relatedTo.Priority}
<br />• Link: {!LEFT($Api.Partner_Server_URL_140,FIND('.com',$Api.Partner_Server_URL_140)+4) + relatedTo.Id}

</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

Workflow Rule-

Workflow Rule Detail  
Rule Name test Object Case
Evaluation Criteria Evaluate the rule when a record is created, and every time it's edited
Active  
Description 
Rule Criteria Case: StatusNOT EQUAL TO null

Email Alert:-
Email Alert Detail  
Description Send email to user Protected Component
Unique Name Send_email_to_user
Email Template Case Email Template
Object Case
From Email Address Current User's email address  
Recipients User: Lakshman
Additional Emails 


The email which I got with all fields merge-
A new Email was added to the case below: 
------------------------ 
• Case #: 00001024 
• Account: United Oil & Gas Corp. 
• Contact Name: Lauren Boyle 
• Case status: Working 
• Priority: Low 
• Link: https://my-dev-ed.my.salesforce.com/50090000001M6mXAAS


Can you check this same example in your org with the same setup ? I think something is missing or preventing to get the fields merge on your side.
Possible solution-
Create a component and reference it in VF template, sample example- https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm

But I am sure that the template should work and we may not be needing to create the component and controller.
Ido Greenbaum 10Ido Greenbaum 10
Thanks for the reply and effort on this Lakshman. 

I took the exact VF Email Template from your example, and received the same result - the Email came in as follows (no Account, Contact Name or Priority): 
A new Email was added to the case below: 
------------------------ 
• Case #: 00007332 
• Account:  
• Contact Name:  
• Case status: In Progress 
• Priority: 
• Link: https://cs61.my.salesforce.com/5004C000000usL2QAI

I followed the link to the Developers Guide 'Using Custom Controllers within Visualforce Email Templates (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm)', and noted the footnote about sharing: 
"Sharing settings are enforced if your email templates use a standard controller. If your organization-wide default for the user object is set to Private and you need to access user information such as name and email address in your Visualforce email template, you can use a custom component or custom controller with the without sharing keywords.
For information about sharing for the user object, see User Sharing Overview in the Salesforce online help."

I set the Case and Account records to be publicly visible under 'organization-wide default', and retested. Again, same results, so this doesn't seem to be the cause. 






 
Ido Greenbaum 10Ido Greenbaum 10
It seems my test was wrong. 
I tested again, and changed my current WorkFlow Rule Criteria which was:
ISCHANGED(Last_Incoming_Email_Content__c)
to: 
ISCHANGED(Status)
After this change, the Email arrived with the populated Merge Fields. 

I believe there's sort of a rece condition with my Custom Field 'Last_Incoming_Email_Content__c' which is causing this, but am not sure why. 

This custom field is being updated in the scope of a different WorkFlow Rule, which simply copy the Case Email Message body to this custom field, through the below Formula (on a 'Field Update' inside the WorkFlow Rule): 
IF(HtmlBody<>"", HtmlBody, TextBody)

I'm looking online to find similar 'WorkFlow Race Condition' topics, with no luck so far.