+ Start a Discussion
Daniel MasonDaniel Mason 

VF Email Template with Related List Invalid Field

Hi SalesForce Community. 

I am hoping you can help,I have encountered different issues with my email template, and i have searched different forums and getting my self more and more confused.

To give some context I am trying to Create a VF email template that returns fields from the related list on the sales_and_marketing__c object.

I have three objects in question (See Two images attached)
sales_and_marketing__c, 
Material_Records__c, 
Materials_Junction__c

When on the sales_and_marketing__c object there is a related list.
When you scroll down to the related list and click on new it opens up a pop up box which is the Materials_Junction__c. (See image below)
User-added image
Materials_Junction__c object has three fields. 

 

Material_Records__c Data Type Master-Detail(Material Records)
Quantity__c  Data Type Number(18, 0)
sales_and_marketing__c Data Type Master-Detail(Sales & Marketing)

When you click on the magifning class on "Materal record" fields you are presenting with the following.
User-added image

Which is this object 

 

Material Records Object

after selecting your materials you will return to this screen(See below) you add in a quanity amount and press save.
User-added image
Once you push "save" the materials you have selected along with quantity will be presented on the  sales_marketing object (as seen below)

User-added image

On my email template i should see .

Name of "Sales and Marketing" Record ( so example above it would be "Test")
Then 5 lines to show the products, item , quantity  

Products and Item comes from the Material_Records__c object, and quantity  from the Materials_Junction__c object

This is my attempt at my VF page 

<messaging:emailTemplate recipientType="Contact"
relatedToType="sales_and_marketing__c"
subject="Testing VF page: {!relatedTo.name}">

<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 materials related to : {!relatedTo.name}.</p>

<table border="0" >
    <tr > 
        <th>Action</th><th>Case Number</th><th>Subject</th><th>Creator Email</th>
    </tr>
    <apex:repeat var="cx" value="{!relatedTo.Materials_Junction__r}">
        <tr>
            <td><a href="https://na1.salesforce.com/{!cx.Materials_Record__c}">View</a> |
            <a href="https://na1.salesforce.com/{!cx.Materials_Record__c}/e">Edit</a></td>
              <td>{!cx.Materials_Record__r.Product__c}</td>
        <td>{!cx.Materials_Record__r.Active__c}</td>
        <td>{!cx.Materials_Record__r.Item__c}</td>
            </tr>
    </apex:repeat>                 
</table>
<p />
     </font>
        </body>
    </html>
</messaging:htmlEmailBody> 

<messaging:plainTextEmailBody>

Dear {!recipient.name},

Below is a list of cases related to Account: {!relatedTo.name}

[ product ] - [ active ] - [ Itemss] 

<apex:repeat var="cx" value="{!relatedTo.Materials_Junction__r}">
[ {!cx.Materials_Record__r.Product__c} ] - [ {!cx.Materials_Record__r.Active__c} ] - [{!cx.Materials_Record__r.Item__c} ]  
</apex:repeat>

For more information login to http://www.salesforce.com
</messaging:plainTextEmailBody>    
</messaging:emailTemplate>

 
Daniel MasonDaniel Mason

Hi All, 

Just an update .

I establied the error i was getting was due to the Materials_Junction__r does not exist. 

I then realised i named the related list name, Materials...thus making the relationship Materials__r not Materials_Junction__r. 

Really appreciate phil in the stackexchange for helping me point this out.

This is my updated code. 

<messaging:emailTemplate recipientType="Contact"
relatedToType="sales_and_marketing__c"
subject="Testing VF page: {!relatedTo.name}">

<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 materials related to : {!relatedTo.name}.</p>

<table border="0" >
    <tr > 

       <th>Product</th><th>Item</th><th>Quantity</th>
        
        
    </tr>
    <apex:repeat var="cx" value="{!relatedTo.Materials__r}">
       [ {!cx.Material_Records__c} ] 

        <tr>
              <td>{!cx.Material_Records__r.Product__c}</td>
              <td>{!cx.Material_Records__r.Item__c}</td>
              <td>{!cx.Quantity__c}</td>
            </tr>
    </apex:repeat>                 
</table>
<p />
     </font>
        </body>
    </html>
</messaging:htmlEmailBody> 

<messaging:plainTextEmailBody >

Dear {!recipient.name},

Below is a list of cases related to Account: {!relatedTo.name}

[Product] - [Item] - [Quantity] 

<apex:repeat var="cx" value="{!relatedTo.Materials__r}">
[ {!cx.Quantity__c} ]   
</apex:repeat>

For more information login to http://www.salesforce.com
</messaging:plainTextEmailBody>    
</messaging:emailTemplate>