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
mamanmaman 

Having trouble showing a related list in Visualforce email template

I've got a custom object Project Team Members related to the standard Opportunity object in a master-detail relationship. I'm trying  to create a Visualforce email template that lists the related list of team members when triggered by a workflow rule on the Opportunity object. Found this sample template:

 

http://wiki.developerforce.com/page/VisualForceEmailTemplates_sample

 

I've tweaked the template to get the job done, but no joy. Getting this error message:

 

        Error: Invalid field Project_Team_Members__c for SObject Opportunity

 

What am I missing? Here's the modificed template:

 

<messaging:emailTemplate

  recipientType="User"

  relatedToType="Opportunity"

  subject="Allocations report for Project: {!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 staff allocated to the project: {!relatedTo.name}.</p>

          <table border="0" >

            <tr >

               <th>Specialist</th>

               <th>PTL</th>

               <th>Billing Role</th>

               <th>Date Assigned</th>

               <th>Date Unassigned</th>

               <th>Comments</th>

            </tr>

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

              <tr>

                <td><a href="https://na1.salesforce.com/{!cx.id}">View</a> |

                <a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a></td>

                <td>{!cx.Specialist_Name__c}</td>

                <td>{!cx.PTL_Indicator__c}</td>

                <td>{!cx.Billing_Role__c}</td>

                <td>{!cx.Date_Assigned__c}</td>

                <td>{!cx.Date_Unassigned__c}</td>

                <td>{!cx.Comments__c}</td>

              </tr>

            </apex:repeat>               

          </table>

          <p />

        </font>

      </body>

    </html>

  </messaging:htmlEmailBody>

</messaging:emailTemplate>

izayizay

Is the master object the Opportunity or the Project_Team_Member__c? The field relating these two objects is in the child object (detail). If the Opportunity object is the master, there should not be a Project_Team_Member__c field which is why you are getting the error.

 

Also, the relatedTo.Project_Team_Members__c, if there is one,  will return only an Id and not a list of records.

 

Have you tried using the<apex:relatedList> tag. Ex: <apex:relatedList id="noteslist" list="NotesAndAttachments" />

 

The list name, list="RelationshipName", is the Child Relationship Name given to the relationship when created. To find this name go to your child object and look for the field that links these two objects.

 

Hope this helps!

 

 

 

mamanmaman

Thanks izay! The master object is Opportunity, so it looks like I got things reversed. I've changed the code so that the relatedToType is "Project_Team_Member__c" (Line 3) and the field on the child object that relates it to the Opportunity master is "Opportunity__c" (Line 28). 

 

But now a different error:

 

        Error: Unknown property 'String.Specialist_Name__c' 

 

I double-checked the spelling, and "Specialist_Name__c" is definitely the API name for a field on the child object Project_Team_Member__c. So I guess I don't understand how the loop is supposed to reference the Project_Team_Member fields. 

 

Thank you again for the much-needed help....here's the full revised code:

 

01 <messaging:emailTemplate
02     recipientType="User"
03     relatedToType="Project_Team_Member__c"
04     subject="Allocations report for Project: {!relatedTo.name}"
05     replyTo="support@acme.com">
06 <messaging:htmlEmailBody>
07     <html>
08     <body>
09          <STYLE type="text/css">
10              TH {font-size: 11px; font-face: arial;background: #CCCCCC;
11              border-width: 1; text-align: center }
12              TD {font-size: 11px; font-face: verdana }
13              TABLE {border: solid #CCCCCC; border-width: 1}
14              TR {border: solid #CCCCCC; border-width: 1}
15          </STYLE>
16          <font face="arial" size="2">
17          <p>Dear {!recipient.name},</p>
18          <p>Below is a list of staff allocated to the project: {!relatedTo.name}.</p>
19          <table border="0" >
20               <tr >
21                    <th>Specialist</th>
22                    <th>PTL</th>
23                    <th>Billing Role</th>
24                    <th>Date Assigned</th>
25                    <th>Date Unassigned</th>
26                    <th>Comments</th>
27               </tr>
28            <apex:repeat var="cx" value="{!relatedTo.Opportunity__c}">
29               <tr>
30                    <td>{!cx.Specialist_Name__c}</td>
31                    <td>{!cx.PTL_Indicator__c}</td>
32                    <td>{!cx.Billing_Role__c}</td>
33                    <td>{!cx.Date_Assigned__c}</td>
34                    <td>{!cx.Date_Unassigned__c}</td>
35                    <td>{!cx.Comments__c}</td>
36               </tr>
37             </apex:repeat>
38           </table>
39           <p />
40          </font>
41         </body>
42       </html>
43     </messaging:htmlEmailBody>
44 </messaging:emailTemplate>

 

izayizay

Now the problem is that {!relatedTo.Opportunity__c} will only return the opportunity id. If you want to display information from the opportunity use the __r sufix to access parent object fields. Ex:  {!relatedTo.Opportunity__r.Specialist_Name__c}.

 

You can show this data like this:

 

<table>

    <tr>

        <th>Specialist: </th><td>{!relatedTo.Opportunity__r.Specialist_Name__c}</td>

    </tr>

    <tr>

        <th>PTL: </th><td>{!relatedTo.Opportunity__r.PTL_Indicator__c}</td>

    </tr>

    <tr>

        <th>Billing Role: </th><td>{!relatedTo.Opportunity__r.Billing_Role__c}</td>

    </tr>

    <tr>

        <th>Date Assigned: </th><td>{!relatedTo.Opportunity__r.Date_Assigned__c}</td>

    </tr>

    <tr>

        <th>Date Unassigned: </th><td>{!relatedTo.Opportunity__r.Date_Unassigned__c}</td>

    </tr>

    <tr>

        <th>Comments: </th><td>{!relatedTo.Opportunity__r.Comments__c}</td>

    </tr>

</table>

 

Hope this helps!

mamanmaman

Actually what I'm trying to give detail on is not fields on the master Opportunity, but fields on the child Project Team Member.

 

Backing up, originally, I thought I could modify the sample template (see again this link:    http://wiki.developerforce.com/page/VisualForceEmailTemplates_sample), with the goal of creating an email that gives related list info pertaining to children of a master object. In the sample template, the master, or at least the main record is an Account, and as I understand it, the email template is designed to give detail on all the associated Cases. I'd like to do a similar thing, but the main record would be an Opportunity, and I'd like to give related list detail for all associate Project Team Members. 

 

Will this template work by in some way swapping out Account/Cases with Opportunity/Project Team Member? Again, the original template:

 

        http://wiki.developerforce.com/page/VisualForceEmailTemplates_sample