+ Start a Discussion

VisualForce Email Template: How to Display Related List Information

Hi All,


I have been trying to follow the example on how to do this - http://wiki.developerforce.com/index.php/VisualForceEmailTemplates_sample


I have a custom object with Contacts as a related list. I wish to be able to list the Contacts within the email template much like the sample above.


I receive the error  "Error: Invalid Field Contacts for SObject customobject__c" using the below:


<apex:repeat var="cx" value="{!relatedTo.Contacts}">


I am assuming the sample is applicable to custom objects. Any suggestions appreciated.



Best Answer chosen by Admin (Salesforce Developers) 
Make sure that your child relationship has a name established (the contacts one).  To do this, go into the child object and edit the look-up/master detail field and make sure there's a value in where it says Child Relationship Name.  Assume you put in "Contacts".  Then you can now refer to it as "Contacts__r" in the template

All Answers




If you create a custom field on a custom object the field name will also end in '__c' ie. you field would be customobject__c.contacts__c.


However I think what you're trying won't work. Only contact, leads, and users can be  recipients of visualforce email templates. Your custom object needs to be related to the Contact object and the contact object the recipientType.


After creating this relationship your emailtemplate would start like this


<messaging:emailTemplate recipientType="Contact"

The email would be sent to the Contact in question and you could then use data in your customobject to populate the merge fields.


To achieve what you're trying to do I suspect you need relationships on each object that link them. Probably a lookup on contact to your object and a master child on your object to contact.




Thanks for the reply.


I don't think I explained myself very well, my bad.


I have used a VF email template with the relatedTo type a custom object with no problem previously, and the recipient type is contact. As below: (Sorry I don't know how to do the code tag in these forums)


<messaging:emailTemplate subject="Test Subject" recipientType="Contact" relatedToType="customobject__c">


The Contact record has a lookup field to the Custom Object which has the related Contacts appear in a related list. What I would like to be able to do is include the names of the related contacts in the email template, which I thought was possible from the sample in my orginal post.


Maybe there is a caveat to using apex:repeat I ammissing?



Did you try using {!relatedTo.Contacts__r} instead?
Thanks for the suggestion, but I got the same error while trying to save the VF email template.
Make sure that your child relationship has a name established (the contacts one).  To do this, go into the child object and edit the look-up/master detail field and make sure there's a value in where it says Child Relationship Name.  Assume you put in "Contacts".  Then you can now refer to it as "Contacts__r" in the template
This was selected as the best answer

Great thank you, I knew it must have been a simple thing. The child relationship name was Contact, I had forgotton to look there. The email template works fine now.


Now where's the blushing emoticon...


Hi all,

I am getting a very similar problem. You can see my trhead here



My code is this one


<messaging:emailTemplate subject="{!recipient.name} belongs to account {!relatedTo.name}"
        recipientType="Contact" relatedToType="Opportunity">
<messaging:smileytongue:lainTextEmailBody >
Dear {!recipient.name},
A text here including the name of the opp: ({!relatedTo.name})

And now we would like to get the product code (or any other field) from the related object: Opportuniy

<apex:repeat var="cx" value="{!relatedTo.OpportunityLineItem}">
              [ {!cx.ProductCode} ] 


And I am always getting "Invalid field xxx for SObject yyy" error; no matter which fields I use

Your relatedto.OpportunityLineItem is incorrect.  It should be relatedto.OpportunityLineItems

Sure, you're right. I've already corrected that and leanred how to show a 'first related list'. But how would I show a second level one?


Starting from account -> show contacts --> and show cases for those contacts 


Starting from Opportunity --> OpportunityLineItems --> and then Product Name (which is in the Product object)




You need to go spend some time with the API documentation to learn more about the table structures.  Also get a schema browser so that you can step thru and know the proper relationships.  In your case for Products as an example it needs to be:


<apex:repeat var="cx" value="{!relatedTo.OpportunityLineItems}">
              [ {!cx.PricebookEntry.ProductCode} ]


To get to the Product Code as an example.


Thanks a lot for your quick response. I will step that time with the API documentation.


Thanks again


I am new to VisualForce... I believe I did all of the steps above, but when I try to Save my VisualForce Email Template I get an "Error: Illegal group reference" and do not know what that means.  Can anyone look at my Email Content below and tell me if you see something wrong?  Thanks in advance.  Namaste  :)


     subject="Trades - Approval Request for {!Case.Buying_Unit__c}"
          <STYLE type="text/css">
TH {font-size:11px; font-face:arial; background: #CCCCCC; border-width:1; text-align:center}
TD {font-size:11px; font-face:verdana}
TABLE {border:solid #CCCCCC; border-width:1}
TR {border: solid #CCCCCC; border-width:1}
          <font face="arial" size="2">

<p>{!Case.Person_Responsible__c} has submitted a Trades form on behalf of {!Case.Buying_Unit__c} that requires your approval.</p>
<p>Trade Number: {!Case.Trade_Number__c}</p>
<table border="0">
<th>Transaction</th><th>Type</th><th>Symbol</th><th>Buy $</th><th>Bond Maturity</th><th>Bond Yield</th><th>Pref Stock Yield</th><th>Sell Shares</th>
<apex:repeat var="cx" value=<"{!relatedTo.Transactions__r}">

<p>================================================================ </p>
<p>You can approve or reject this request from your desktop email program or mobile device:</p>
<p>1. Click the Reply button </p>
<p>2. Type the word APPROVE or REJECT in the first line of the reply </p>
<p>3. Type any additional comments in the second line of the reply </p>
<p>4. Click Send</p>
<p>================================================================ </p>
<p>Click here: {!ApprovalRequest.Internal_URL} </p>
<p>Please note that the approval history and any comments you enter will be visible to other employees.</p>


Thank you very much, dkorba2k5. Finally I could understand what to do.
Kunaal Patodia 6Kunaal Patodia 6
This is my VF Email Template and i get an Error 
"Error: Invalid field PriceGroupAccounts for SObject Account"

<messaging:emailTemplate subject="Welcome" recipientType="User" relatedToType="Account">
<messaging:plainTextEmailBody >
This is your new Visualforce Email Template.
UserID:<apex:repeat value="{!relatedTo.PriceGroupAccounts}" var="item"/>

In my custom object , which is a child Object of Account , i looked at the Account Field , which is master-child relationship and copied the Child Relationship Name , which is  PriceGroupAccounts ??