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
Alexis KasperaviciusAlexis Kasperavicius 

Related list in VisualForce Email template - custom objects possible?

Hi All -

 

I am new, so please forgive my green - but I am having a perplexing problem in generating a Visual Force email template showing related lists for a custom object.

 

I CAN get a list of Cases related to an Account with no problem, but when I put in a custom object  (Child Relationship Name "Suppliers") which is also (Master-Detail) related to Account I get the following error: Error: Invalid field Suppliers for SObject Account

 

I have tried everything I can think of - using API name instead, appending "__c" to the end, but it seems that i can't make any lists at all with custom objects in this code.

 

Is there a restriction within this type of code that allows only lists with standard objects or is there something I'm missing?

 

Any help would be greatly appreciated. I am not good enough to make a custom Apex controller yet and a little under the gun to kluge something together.

 

Thanks much,

 

Lex

 

Below is relevant code:

 

 

<messaging:emailTemplate recipientType="Contact"
    relatedToType="Account"
    subject="Supplier report for Account: {!relatedTo.name}"
    replyTo="support@acme.com" >
<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>Dear {!recipient.name},</p>
      <p>Below is a list of supplier records related to the account: {!relatedTo.name}.</p>
<table border="0" >
        <tr >
            <th>Action</th><th>Product</th><th>Status</th><th>Creator Email</th><th>BL date</th>
         </tr>
<apex:repeat var="cx" value="{!relatedTo.Suppliers}">
   <tr>
       <td><a href="https://na1.salesforce.com/{!cx.id}">View</a> |  
       <a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a></td>
(...snip...)

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

I think you want the relationship field, so relatedTo.suppliers__r

All Answers

jwetzlerjwetzler

I think you want the relationship field, so relatedTo.suppliers__r

This was selected as the best answer
Alexis KasperaviciusAlexis Kasperavicius

Bless you my saviour! ;)

 

Thank you so much for responding, you have just solved a problem that has been holding me up for three days!!!

 

Thank you! Thank you! Thank you!

 

Alex

Peter vSPeter vS

This seems to only display the one related record. How do I display all of the records related to an object within a related list.

Sushant.ChavanSushant.Chavan

Visual Force Email Templates with Related List only works in Preview Mode. When the Email Alert is triggered using a workflow , the Email contains no data.

BarracksBarracks
I have the same issue, but where can I find the name of the relationship?

I have a custom object called Project__c (Master-Detail with Account) and I tried 'projects__r' and 'project__r' but I still get this error:
Error: Invalid field project__r for SObject Account
Alexis KasperaviciusAlexis Kasperavicius
The name is on the child object. Go the the child object and find the lookup field to the parent object. Edit that field and you will see 'Child Relationship Name’ which is the name you’re looking for. Just add __r to that name and you should be good to go. A
BarracksBarracks
Thank you Alex
Mani Anand 10Mani Anand 10
I have a similar issue too: The child relationship name is Quotes but i'm still getting the error. Help appreciated.

<messaging:emailTemplate subject="Quote Information" recipientType="User" relatedToType="Opportunity">
<messaging:plainTextEmailBody >


</messaging:plainTextEmailBody>
 <messaging:attachment renderAs="pdf">
<head>
<style>
@page {
  size: auto;   /* auto is the initial value */
  margin-top: 0.0%;
  margin-left: 4mm;
  margin-right: 4mm;
  margin-bottom: 4mm;
}
td{
font-family:Arial,Helvetica,sans-serif;
font-weight:normal;
font-size:15px;
}
.tds{
font-family:Arial,Helvetica,sans-serif;
font-weight:normal;
font-size:15px;
}
td.normal{
font-family:Arial,Helvetica,sans-serif;
font-weight:normal;
font-size:15px;
}
td.nbold{
font-family:Arial,Helvetica,sans-serif;
font-weight:bold;
font-size:15px;
}
</style>
</head>
The Discount on the Quote exceeds the maximums alloted and hence requires approval.

Below is the Opportunity information:
Opportunity Name : {!relatedTo.Name}
Opportunity Account : {!relatedTo.Account}
Close Date: {!relatedTo.CloseDate}
Stage: {!relatedTo.StageName}
Amount: {!relatedTo.Amount}
Link: {!relatedTo.Link}
    <tr>
       <td  width = "14%"><b>Quote Number</b></td>
       <td  width = "10%"><b>Quantity</b></td>
       <td  width = "44%"><b>Expiration Date</b></td>
       <td  width = "44%"><b>Discount</b></td>
       
    </tr> 
    <apex:repeat value="{!relatedTo.Quotes__r}" var="line">
    <tr>
      <td>{!line.QuoteNumber}"/></td>
      <td>{!line.Name}"/></td>
      <td>{!line.ExpirationDate}"/></td>
      
   
    </tr>
    </apex:repeat>
    
    <tr><td>&nbsp;</td></tr>        
    <tr><td>&nbsp;</td></tr>
     
        
</messaging:attachment> 

</messaging:emailTemplate>
Sonali Kamra 16Sonali Kamra 16
Mani, if you are using a managed package then try using the Namespace Prefix of the package along with the Child Relationship Name. For example, in my case the child relationship name was 'Quote_Rate_Plan_Charges' and the namespace prefix was 'zqu' - I used '{!relatedTo.zqu__Quote_Rate_Plan_Charges__r}' and it worked.
Juan Miguel Mendoza CruzJuan Miguel Mendoza Cruz
Thank you, Sonall, I had the same problem and could not understand what was happening in my organization and the package was the problem. Appending the namespace prefix solved the problem.
EGA FuturaEGA Futura
https://medium.com/@cynthiabellmcgillis/todays-the-day-write-your-first-visualforce-email-template-fbbdeec2592c