+ Start a Discussion
girbotgirbot 

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}">
        {!cx.FirstName}
        {!cx.LastName}
</apex:repeat>

 

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

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dkorba2k5dkorba2k5
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

wesnoltewesnolte

Hey

 

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"
relatedToType="CustomObject__c"

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.

 

Wes

girbotgirbot

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?

 

 

dkorba2k5dkorba2k5
Did you try using {!relatedTo.Contacts__r} instead?
girbotgirbot
Thanks for the suggestion, but I got the same error while trying to save the VF email template.
dkorba2k5dkorba2k5
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
girbotgirbot

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

dani-mestrelabdani-mestrelab

Hi all,

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

http://forums.sforce.com/sforce/board/message?board.id=Visualforce&thread.id=15170

 

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} ] 
</apex:repeat>

 

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

dkorba2k5dkorba2k5
Your relatedto.OpportunityLineItem is incorrect.  It should be relatedto.OpportunityLineItems
dani-mestrelabdani-mestrelab

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?

e.g:

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

or

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

 

Thanks!

dkorba2k5dkorba2k5

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} ]
</apex:repeat>

 

To get to the Product Code as an example.

dani-mestrelabdani-mestrelab

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

 

Thanks again

datadivadatadiva

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  :)

 

<messaging:emailTemplate
     subject="Trades - Approval Request for {!Case.Buying_Unit__c}"
     recipientType="Contact"
     relatedToType="Transactions__r">
<messaging:htmlEmailBody>
<html>
     <body>
          <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}
          </STYLE>
          <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>
<p>Trade Number: {!Case.Trade_Number__c}</p>
<table border="0">
     <tr>
<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>
     </tr>
<apex:repeat var="cx" value=<"{!relatedTo.Transactions__r}">
     <tr>
<td>{!cx.Name}</td>
<td>{!cx.Trade_Type__c}</td>
<td>{!cx.Symbol_Name__c}</td>
<td>{!cx.Buy_Amount__c}</td>
<td>{!cx.Bond_Maturity_Date__c}</td>
<td>{!cx.Bond_Yield__c}</td>
<td>{!cx.Preferred_Stock_Yield__c}</td>
<td>{!cx.Shares__c}</td>
     </tr>
</apex:repeat>
</table>

<br>
<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>
<p>
<p>Click here: {!ApprovalRequest.Internal_URL} </p>
<br>
<p>Please note that the approval history and any comments you enter will be visible to other employees.</p>

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

hugogmendeshugogmendes
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 >
Congratulations!
This is your new Visualforce Email Template.
UserID:<apex:repeat value="{!relatedTo.PriceGroupAccounts}" var="item"/>
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

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