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
John Upton 8John Upton 8 

How to use Visualforce email template with 'Related To' relationship.

I have created a table to house basic information for putting into an email (subject, intro etc). I will call this EMAIL. I have created a child table to house the content for emails, so each EMAIL object can have one or more CONTENT objects.
I have created a many-many relationship between CONTACT and EMAIL via another table called LINK.
I have two email templates. One is 'Custom' and as follows. This works fine, when I trigger it (by adding a LINK object),  I get an email send to my Contact with both the expected merge fields filled in.
Dear {!CONTACT.FirstName}, Intro: {!EMAIL__c.Introduction__c}
However, I have a second template created using Visualforce (below). If I switch my email alert to use it instead, then perform the same action to get the email to be sent to a Contact, neither the Contact's Name or the EMAIL Introduction is merged in! Also note that, if I use this template and 'Send Test and Verify Merge Fields', entering my Contact and EMAIL ID causes the merge from both objects to work perfectly! Can anyone offer any advice?
Thanks
<messaging:emailTemplate recipientType="Contact"
    relatedToType="Queued_Email__c"
    subject="ed test 3">
    <messaging:htmlEmailBody >
        <html>
            <body>
            <p>Dear {!recipient.name},</p>
            <p>{!relatedTo.Introduction__c}</p>
            </body>
        </html>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
 
JeffreyStevensJeffreyStevens
Someone else may jump in here - but I had similar issues - I finally created a coponent and a controller for the html email template.  So, the html email template looked like this...
<messaging:emailTemplate subject="ed test 3" recipientType="Contact" relatedToType="Queued_Email__c" >
<messaging:htmlEmailBody >
      
    <html>
        <body>
            
            <c:emailTemplateComponent recipient_id="{!recipient.id}" relatedTo_id="{!relatedTo.id}"></c:emailTemplateComponent>

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

then the component (named emailTemplateComponent) looked like this...
<apex:component controller="ctrl_QueuedEmail" access="global">
    <apex:attribute type="id" name="recipient_id" assignTo="{!recipientId}" description="Recipient" />
    <apex:attribute type="id" name="relatedTo_id" assignTo="{!relatedToId}" description="RelatedTo" />

<body>
<p>Dear {!recipient.name},</p>
<p>{!relatedTo.Introduction__c}</p>

....

Then the controller should have something like this....
public with sharing class ctrl_QueuedEmail {

 public string recipientId;
 public string relatedToId;

 public Contact recipient {get;set;}
 public CONTENT__c relatedTo {get;set}

  public string getRecipientId() {return recipientId; }
  public void setRecipientId(string inString) {
    recipientId = inString;
    recipient = [SELECT id,name FROM Contact WHERE id = :recipientId];
  }

  // RelatedToId
  public string getRelatedToId() { return relatedToId;}
  public void setRelatedToId(string inString) {
    relatedToId = inString;
    relatedTo = [SELECT id,Introduction__c FROM Queued_Email__c WHERE eMail__c.id = :recipientId]
  }

  public ctrl_QueuedEmail() {
    // Whatever code you need in the constructor....
  }

}

It's not all exact code - but the concept is that the related and recipient IDs are passed to the component and to the controller.  Then you can do stuff with them in the controller.

Like I said - someone else might have a better or more precise solution. 
 
ChellappaChellappa
Hi John and Jeffrey,
Hope you are working in Salesforce platform :) , since this is a 3 year old discussion
I have the same situation where RelatedTo.Id is passed to the controller and the code is in tact.
Like John mentioned, it is working in the 'Send Test' preview, but the actual email is not sending the info.
Any pointers much appreciated!
Thanks,
Chellappa
Brian Good 8Brian Good 8
I am also coming across the same problem.  My relatedTo fields will populate when verifying the Merge Fields in system setup, as well as in a test email.  However, when the email comes through from the system firing my workflow, the fields are not being merged.
Jack Pond 2Jack Pond 2
I've tried 2 Modes for using Visualforce Email Templates(with embedded components) - 1) From email alert and 2) from Apex Action.  Anytime I try to reference {!relatedTo.id}, receive error:  invalid cross reference id.  Both Approaches.  Would use the component controller method except any 'relatedTo' reference as component attribute also causes error.  As with Brian Good 8 (above), "Send Test and Verify Merge Fields" works perfectly.

Could embed a link to the "relatedTo" object(Case) in the recipient object (Contact), but that is very kludgey and risks other performance and maintainability issues.
Kavitha Arvind 16Kavitha Arvind 16
I had an issue with Visualforce Email Template referencing related fields, so I created a formula field in the object that the template was referencing and it worked fine.
skompoltskompolt
Same issue for me....   Works fine in Sandbox and Production with the Email Merge Preview. Even looks perfect in the preview with a Manuall Select Template. Result in email client missing related list.