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
Zafar IssadeenZafar Issadeen 

Create a VisualForce email template with a related list based on criteria

I am a little green to Salesforce so I apologise in advance if this is a simple hack that I did not know. 

Scenario: I would like to email client's weekly with the status of their milestones of a project. Each Project has multiple milestones. The additional layer of complexity is that some milestones are internal (and we have a checkbox called Publicly Tracked to filter for the external facing milestones). How do I create a template that show the related milestones for a project where Publicly Tracked = Yes

I have been following the tutorial on https://www.vandeveldejan.com/tips/for-admins/15-the-not-so-scary-visualforce-email-template-as-order-confirmation-email
Andy Kallio 7Andy Kallio 7
HI. I don't have experience with emails. It looks different than a regular visualforce page. With a regular visualforce page you would have to use a custom controller or a controller extension to get your list. With email, it looks like you will have to write your controller for a visualforce component, and then put that component into your email template. Here is some documenation on that: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm

Sounds like you may also need to read up on standard controllers, custom controllers and controller extensions. If you did not need the criteria it would be very easy to simply use a standard controller. For example, if you defined a page's standard controller to be 'Quote' then you could easily access the Quote Line Items via the child relationship Quote.QuoteLineItems. However, since you need the filter you need to make your own controller, and since this is an email it looks you will have to do that in a custom component

 
<apex:page standardController="Quote" >
            
    
                    <table class="qitable">
                        <tr>
                            <th class="row-name label" >Product</th>
                            <th class="row-date label">Description</th>
                            <th class="row-date label">Quantity</th>
                            <th class="row-status label">CPI</th>
                            <th class="row-status label">Total</th>
                        </tr>

                        <apex:repeat var="qli" value="{!Quote.QuoteLineItems}">
                           
                            <tr>
                                <td class="row-name detail" >{!qli.Product2.Name}</td>
                                <td class="row-name detail" >{!qli.Description}</td>
                                <td class="row-name detail" >{!qli.Quantity}</td>
                                <td class="row-date detail">
                                    <apex:outputText value="{0, number,currency}">
                                        <apex:param value="{!qli.UnitPrice}"/>
                                    </apex:outputText>
                                </td>
                                <td class="row-date detail">
                                    <apex:outputText value="{0, number,currency}">
                                        <apex:param value="{!qli.TotalPrice}"/>
                                    </apex:outputText>
                                </td>
                            </tr>
                        </apex:repeat> 
                        <tr class="label">
                            <td colspan="4" class="alignRight">Total:</td>
                            <td>
                                <apex:outputText value="{0, number,currency}">
                                    <apex:param value="{!quote.TotalPrice}"/>
                                </apex:outputText>
                            </td>
                        </tr>
                    </table>
    

   
   
    
</apex:page>