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 ?

  • and the problem is?
    – PepeFloyd
    Commented Dec 20, 2013 at 12:15
  • That is only problem how to fetch all the object values into one email template is it possible in apex class or any other
    – user4955
    Commented Dec 20, 2013 at 12:28
  • welcome to salesforce.stackexchange. Your question will become more clear if you update it to include what you already know or have tried yourself. Especially the filtering and condition part may need further explanation. Commented Dec 20, 2013 at 12:35
  • what my question is i want an vf email template, in that the values should be come from custom object and the values to be fetch based on the country(i have loss object in that city is dade so all the values of dade should be display while sending email to customer) so help me
    – user4955
    Commented Dec 20, 2013 at 12:41
  • Ive tried to re-write your question so that it may be easier to answer. Feel free to rollback if you disagree. If you've already started working on a vf template it will help if you update your question with the code. Commented Dec 20, 2013 at 12:52

3 Answers 3


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:facet name="header">Account Name</apex:facet>

Create your template that then uses the component:

<messaging:emailTemplate subject="Embedding Apex Code" recipientType="Contact" relatedToType="Opportunity">
        <p>As you requested, here's a list of all our US accounts:</p>
        <p>Hope this helps with the {!relatedToType}.</p>

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 title="Dimensions">
              <apex:repeat value="{!fields}" var="f">
                  <apex:outputField value="{!merch[f.fieldPath]}" 


  • This is ok but in every record will display with some values (ie,1)us: name,age,email,phone.....2)us1:name,age,phone...)like that one after the above that means total info of record should display for every account.
    – user4955
    Commented Dec 20, 2013 at 13:10
  • and also i will add some field in vf page so i need to display that fields also is it possible with this
    – user4955
    Commented Dec 20, 2013 at 13:46
  • Yes. You can add any apex code in the controller and use it in your component. Commented Dec 20, 2013 at 13:49
  • you just give me some example code for custom object not for standard i can get easily i think,thanks
    – user4955
    Commented Dec 20, 2013 at 14:07
  • This error i am getting what can i do :::Method does not exist or incorrect signature: [Schema.DescribeSObjectResult].getFields() at line 9 column 16
    – user4955
    Commented Dec 20, 2013 at 14:34

To create a new template, go to Setup → (Administration Setup) Communication Templates → Email Templates and click New Template

You then have 4 options:

  1. Text - The email is text only: not flashy, but it get's the job done. This is a great place to master displaying Merge Fields.
  2. 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.

  3. 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.

  4. 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...).

  • could you please give me more info about that by using export report as attachment we can create like email template
    – user4955
    Commented Dec 20, 2013 at 16:52
  • Go check out my answer to the question I've linked to. It's a hack but it works pretty well. Check salesforce.stackexchange.com/questions/11948/… too. Essentially my answer gives you a VF component that can be a part of email template. Then you write a scheduled job that will send an email with this template out... I needed it because it really had to be an attached CSV. Your question looks like you'd be happy with standard "schedule report" (go to report, click the down arrow next to [Run Report Now]), will say "Schedule future runs.."
    – eyescream
    Commented Dec 20, 2013 at 17:47
  • But if you know that you'll never need more than 2,000 records (check for most commonly used city etc?) I'd strongly recommend giving the Analytics API a go: developer.force.com/releases/release/Winter14/AnalyticsAPI, salesforce.com/us/developer/docs/api_analytics/…
    – eyescream
    Commented Dec 20, 2013 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .