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
Glenn at MvistaGlenn at Mvista 

Display data from unrelated list in email template using Apex Class

We have a VisualForce Email Template where we want to display a
related list.  If it was a regular VisualForce page, we would include
an APEX extension and then call the class to populate a dataTable like
this:

 <apex:dataTable value="{!OppLines}" var="opp" border="0"
cellpadding="1" cellspacing="0">
    <apex:column ><apex:outputField
value="{!opp.PriceBookEntry.name}"/></apex:column>
    <apex:column ><apex:outputField value="{!opp.LSP__r.name}"/></apex:column>
    <apex:column ><apex:outputField value="{!opp.Quantity}"/></apex:column>
 </apex:dataTable>

 Which calls an APEX controller with this code:

 public OpportunityLineItem[] getOppLines() {
   OpportunityLineItem[] siItems = [SELECT PriceBookEntry.name,
LSP__r.name, Quantity
        FROM OpportunityLineItem
        Where OpportunityId = :ApexPages.currentPage().getParameters().get('Id')
        Order by PricebookEntry.name];
   return siItems;
 }

However, there is no way that we can see to attach the APEX controller
as an extension.  The only thing we could come up with is using a
component, and then referencing it in the VisualForce Email Template
like this:

<c:Opp_Lines_Component />

Unfortunately, the code in the APEX controller which pulls the ID from
the current VF Page doesn't work in an email template and so the
dataTable is always blank.

Is there any way to do what we are trying to do?

gtuerkgtuerk

Add an Apex:Attribute to the Component and pass the foreign key ID into the component.  Then make the component hold the controller extension.  Let me know if you need more detail for that.

Glenn at MvistaGlenn at Mvista

This makes sense, but a little more detail to give me a head start would be great.