I have a visualforce component
which is used in an email template
to display a list of related items.
Visualforce Component
<apex:component controller="LeadProductsController" access="global">
<apex:attribute name="parentId" type="Id" assignTo="{!leadId}" description="" />
<apex:attribute name="products" type="Lead_Product__c[]" description="" />
<table style="width:500px" cellpadding="3" cellspacing="0" border="0">
<thead style="background-color: #003366; color: #ffffff;">
<td>Name</td>
<td>Code</td>
<td>QTY</td>
<td>Sales Price</td>
<td>Total Price</td>
</thead>
<tbody>
<apex:repeat value="{!products}" var="p">
<tr>
<td>{!p.Product_Name__c}</td>
<td>{!p.Product_Code__c }</td>
<td>{!p.Quantity__c}</td>
<td>0</td>
<td>0</td>
</tr>
</apex:repeat>
</tbody>
</table>
</apex:component>
Apex Controller
public without sharing class LeadProductsController {
public List<Lead_Product__c> products { get; private set; }
public String leadId;
public void setLeadId(Id leadId) {
this.leadId = leadId;
products = [SELECT Id, Product_Name__c, Product_Code__c, Quantity__c FROM Lead_Product__c WHERE Lead__c = :leadId];
}
public Id getLeadId() {
return leadId;
}
}
I then use the component in the email template like this:
<c:LeadProducts parentId="{!relatedTo.Id}" ></c:LeadProducts>
I've not used visualforce component
's before, so I suspect I am doing many things wrong, but I cannot find a decent example of what I am trying to achieve.
How should I get a List of data from a controller when using a visualforce component
?