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

Referencing related custom objects in VisualForce Email Templates?

Hi everyone -

I'm trying to use the example of a VisualForce Email Template on Accounts with related Cases to create an email for one custom object and its related list items.  Referencing the first object is straightforward, but I haven't been able to figure out how to reference the related list.

<messaging:emailTemplate subject="A foo is required for this case; Do not reply to this email." recipientType="User" relatedToType="foo__c">
<messaging:plainTextEmailBody >

   Case/foo Number: {!relatedTo.Case_Number__c}
   Subject: {!relatedTo.Subject__c}
   // Here's where the trouble occurs, where foo_item__c is an individual
   // item in a related list
   <apex:repeat var="cx" value="{!relatedTo.foo_Line_Item}">
       {!cx.Detail_Number__c} - {!cx.Replacement_Component__c}

   // whether I use foo_Line_Item__c, foo_Line_Items, foo_Line_Item__cs - nothing seems to apply

Looking at the schema, the foo__c child object name is foo_Line_Item__c.


I found this thread related to a custom child of Accounts, suggesting to
  • on the Child object, make sure that there is a value in the Child Relationship Name of the Lookup field so that it can be referenced as foo.fooLineItems (if fooLineItems is the Child Relationship Name)
  • or to change the reference from relatedTo.foo_Line_Item__c to relatedTo.foo_Line_Item__r
I added the Child Relationship Name 'fooLineItems', but I am still getting an error in the Email Template of Error: Invalid field fooLineItems for SObject foo__c.  Well, yeah - there is no field named fooLineItems, but there is a child relationship with that name.

I cancelled out of the template update and went back in, thinking that the relationship hadn't registered somehow, but there is no change.  Replacing __c with __r did not effect any change either, resulting in Error: Invalid field foo_Line_Item__r for SObject foo__c.

Let me know if you come up with something, and thanks!

(you know that if it isn't resolved here, I am going to be bugging someone at DreamForce about it :)  )

Message Edited by finalist on 10-28-2008 07:28 AM
Okay, the connection has to be made between both of the previous suggestions -- once the Child Relationship Name is in place, then that relationship can be referenced using
the __r syntax (i.e. fooLineItems__r).

So if the Child Relationship Name is fooLineItems, then the reference in the VisualForce Email Template needs to be
<apex:repeat var="cx" value="{!relatedTo.fooLineItems__r}">
    <table border="0">
               <td>{!cx.Serial_Number__c} - {!cx.Replacement_Hardware_Component__c}</td>

 I hope this is useful!  (alas, I must credit MJ for connecting the dots; I'm merely the documentarian here.)

Yes, for now you need to explicitly set the name of the Child Relationship (we'd like to default this in the future so you don't have to do this).  Then you refer to it using the __r relationship.

Also I believe there's a limitation currently that the related list has to be present on your page layout for it to be available for use.

Thank you for the helpful information.

After getting this far (being able to reference custom correctly objects in vf email templates), we are still unable to get visualforce templates to merge data when being called by a workflow rule email alert.  The data merges into the visualforce template beautifully when viewed in Preview mode or when the template is manually selected from within a record using Send An Email button under Activity History related list.  However, when the template is called from a workflow rule, the template is sent blank.  Any thoughts?

Thank you!

Very well ... now how can we add filters on the child records before displaying them in the email body?
For example, I want to show only those fooLineItems which are active.

One way, I think of, is to use <table>,<apex:repeat>,<tr> and java script to conditionally add rows using DOM.

Would someone please propose a better method?

Life would so easy if we could use a controller extension as we do in VF pages!

You can write a custom controller with a getter which filters your data. I.e. Standartcontroller = Account extension="filteredData".


In this controller you have to write a getter which return your needed data.


When you say explicity set the name, do you mean you have to call it "CustomObJjectLineItems"?