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
Chris Leszka 5Chris Leszka 5 

Visualforce email - how to reference list of opportunities/line items from a custom object

Hello, In our org we have different divisions each with their own sales reps that all sell to the same accounts.  Once a sale has been made, the Account Management team steps in and takes over and is in charge of maintaining renewals for the account.  Because products are broken out between different opportunities, the team has struggled to find an efficient way to let a customer know which of their subscriptions are coming up for renewal, without having to manually piece together the details from multiple opportunities manually.

To solve for this, I created a custom object call Renewal_Package__c.  This is a simple object that allows the team to link all related opportunities for an account to a package and then ideally, be able to generate an email with the details of all those opportunities broken out within it.  Renewal_Package__c has a Master-Detail relationship with Account, and a couple other custom fields where the user can enter in a date and a note about the package.  Because you can't have a Master-Detail relationship with the Opportunities object, i have a lookup field on Opportunities where you can lookup the Renewal Package and link it appropriately.  This lookup, then populates to the product level as well via a workflow rule.  With this setup, you can look at a Renewal Package record and see the related list for Opportunities as well as all the individual products from the opportunities.

Now the hard part - I'm trying to setup a Visualforce email template that pulls in a related list of all of those Opportunity Products.  Here is my code:
<messaging:emailTemplate subject="Your Upcoming Renewal" recipientType="User" relatedToType="Renewal_Package__c">
<messaging:htmlEmailBody >
<html>
<body>

 <br>Your subscriptions are expiring soon!  Please review the list of products below and let us know whether you'll be continuing your subscription with us, so that we can ensure you have no disruptions in access to the data you rely on.   </br>

 <h3>Expiring Renewable Products</h3>                  
        <table border="3" cellpadding="10" width="100%" >
            <tr > 
               <th>Product Name</th><th>Quantity</th><th>Unit Price</th><th>Start Date</th><th>End Date</th><th>Additional Details</th><th>Renewal Approved</th>
           </tr>
        <apex:repeat var="opp" value="{!relatedTo.Renewal_Package__r.Opportunitylineitems}">
           <tr>
               <td align="center">{!opp.PriceBookEntry.name}</td>
               <td align="center">{!ROUND(opp.Quantity,0)}</td>
               <td align="center">
                   <apex:outputText value="{0,number,$###,###,##0.00}">
                       <apex:param value="{!ROUND(opp.UnitPrice,0)}" />
                   </apex:outputText></td>                                                                
               <td align="center">
                   <apex:outputText value="{0,date, M/dd/yyyy}">
                       <apex:param value="{!opp.ServiceDate}" />
                   </apex:outputText></td>
               <td align="center">
                   <apex:outputText value="{0,date, M/dd/yyyy}">
                       <apex:param value="{!opp.Service_End_Date__c}" />
                   </apex:outputText></td>                   
               <td align="center">{!opp.Description}</td>  
               <td align="center"></td>                            
           </tr>
         </apex:repeat>
         </table>        
<p/>
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

The error I receive is "Error: Invalid field Renewal_Package__r for SObject Renewal_Package__c", so it's failing at the first lookup in the related list table: <apex:repeat var="opp" value="{!relatedTo.Renewal_Package__r.Opportunitylineitems}">.

Is it possible to reference the opportunity and/or Opportunity products with how i have everything setup?  I've tried a few variations of the code, but it errors every time.

Any help would be appreciated.
Thank you in advance!
sridharbsridharb
Hi Chris,

Add your controller code if possible. New pair of eye most of the times finds a small missing things..Worked for me lets hope works for you.

Regards
Sri
Chris Leszka 5Chris Leszka 5
Hi Sri,
Since this is setup as a VisualForce Email Template, that's all the code there is for it unfortunately.