public class findSmithAccounts { private final List<Account> accounts; public findSmithAccounts() { accounts = [select Name from Account where Name LIKE 'Smith_%']; } public List<Account> getSmithAccounts() { return accounts; } }
接下来,创建一个名为smithAccounts的自定义组件,该组件使用此控制器:<apex:component controller="findSmithAccounts" access="global"> <apex:dataTable value="{!SmithAccounts}" var="s_account"> <apex:column> <apex:facet name="header">Account Name</apex:facet> {!s_account.Name} </apex:column> </apex:dataTable> </apex:component>
提示:请记住,Visualforce电子邮件模板中使用的所有自定义组件必须具有全局访问级别。 最后,创建一个包含smithAccounts组件的Visualforce电子邮件模板:<messaging:emailTemplate subject="Embedding Apex Code" recipientType="Contact" relatedToType="Opportunity"> <messaging:htmlEmailBody> <p>As you requested, here's a list of all our Smith accounts:</p> <c:smithAccounts/> <p>Hope this helps with the {!relatedToType}.</p> </messaging:htmlEmailBody> </messaging:emailTemplate>
请注意,尽管emailTemplate组件需要relatedToType属性,但它对此示例没有任何影响。它具有“机会”的价值只是为了表明它可以采用与自定义组件中使用的对象不同的对象值。 注意: 如果您的电子邮件模板使用标准控制器,则强制共享设置。如果用户对象的组织范围默认设置为“私人”,并且您需要访问Visualforce电子邮件模板中的用户信息(如姓名和电子邮件地址),则可以使用带有不共享关键字的自定义组件或自定义控制器。 有关共享用户对象的信息,请参阅Salesforce联机帮助中的用户共享概述。