I want to create an email template for custom object. It should display certain fields of the custom object depending on some conditions like the value of the city or country fields.
How can I do this ?
I want to create an email template for custom object. It should display certain fields of the custom object depending on some conditions like the value of the city or country fields.
How can I do this ?
Take a look at creating a Visualforce Email Template.
Pay attention to the page on Using Customer Controllers with Visualforce Email Templates. From there:
Create your custom controller:
public class FindAccountsController {
private final List<Account> accounts;
public FindAccountsController() {
// Query however you want, based on Country for example
accounts = [select Name from Account where ShippingCountry = 'US'];
}
public List<Account> getAccounts() {
return account
}
}
Create your component:
<apex:component controller="FindAccountsController" access="global">
<apex:dataTable value="{!accounts}" var="account">
<apex:column>
<apex:facet name="header">Account Name</apex:facet>
{!account.Name}
</apex:column>
</apex:dataTable>
</apex:component>
Create your template that then uses the component:
<messaging:emailTemplate subject="Embedding Apex Code" recipientType="Contact" relatedToType="Opportunity">
<messaging:htmlEmailBody>
<p>As you requested, here's a list of all our US accounts:</p>
<c:AccountsComponent/>
<p>Hope this helps with the {!relatedToType}.</p>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
To build the list of all fields dynamically (i.e., SOQL equivalent of SQL's Select *) take a look at Apex Describe and Dynamic SOQL.
You could use a Field Set if you want a subset of all of the fields. The beauty of this approach is it allows your admins (non devs) to maintain the application you build through clicks and not editing your Visualforce directly.
Here's an example using a custom controller from the docs that builds a SOQL query from a Field Set and then displays the results in a component.
public class MerchandiseDetails {
public Merchandise__c merch { get; set; }
public MerchandiseDetails() {
this.merch = getMerchandise();
}
public List<Schema.FieldSetMember> getFields() {
return SObjectType.Merchandise__c.FieldSets.Dimensions.getFields();
}
private Merchandise__c getMerchandise() {
String query = 'SELECT ';
for(Schema.FieldSetMember f : this.getFields()) {
query += f.getFieldPath() + ', ';
}
query += 'Id, Name FROM Merchandise__c LIMIT 1';
return Database.query(query);
}
}
And component:
<apex:component controller="MerchandiseDetails">
<apex:form >
<apex:pageBlock title="Product Details">
<apex:pageBlockSection title="Product">
<apex:inputField value="{!merch.Name}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Dimensions">
<apex:repeat value="{!fields}" var="f">
<apex:outputField value="{!merch[f.fieldPath]}"
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:component>
SObjectType.Merchandise__c.FieldSets.Dimensions.getFields();, make sure that you have created your Field Set first. In the above example the Field Set is called Dimensions.
– Peter Knolle
Dec 20 '13 at 14:38
SObjectType.Property__c.FieldSets.Dimensions.getFields() will fail if you do not have a Field Set called Dimensions on the object Property__c. So, create your Field Set, for example call it My_Field_Set and then reference it like SObjectType.Property__c.FieldSets.My_Field_Set.getFields()
– Peter Knolle
Dec 20 '13 at 16:04
To create a new template, go to Setup → (Administration Setup) Communication Templates → Email Templates and click New Template
You then have 4 options:
Merge Fields.HTML (using Letterhead) - You can use HTML but then also use the structure of using one of Salesforce's Letterheads. This is the standard option, and I recommend any user new to templates check this option out first.
Custom (without using Letterhead) - aka HTML (without Letterhead). If you are comfortable with HTML to the point that you prefer its styling capabilities, then this is the option for you. All you can write is HTML, so you'll have full control over the content, look, & feel of the Email Template.
Visualforce - Sometimes, simply displaying Merge Fields isn't enough, and we are requested to do some calculations and use those calculations in the Email we are trying to send. @Peter Knolle's answer is a great primer for using this option as an Email Template. It is by far the most versatile of all the options though it requires programming. If you are a developer, this option probably-should be in your repertoire.
Check out the Salesforce Help Portal (help.salesforce.com) for more information.
Also, some specific documentation on creating HTML Email Templates.
Why do you feel like you need an email template? Have you already explored the option to export reports? You can schedule such exports to have them emailed to users (the data is embedded into the email as HTML and limited to first 2K rows though).
Roughly speaking email templates can be related to 1 record. Sure, it might include this records' related list(s) but still... you start with one. To fetch more data you'll need components. Either in the way explained in Mike's answer or maybe in the nasty trick from Scheduled reports as attachment (I'm still not too proud with the dirty hack I've used there but as long as the newly introduced Analytics API will continue to have the 2K records limit...).