Email Template Creation
-
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 ?
-
Answer:
Take a look at creating a http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_templates_intro.htm. Pay attention to the page on http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_templates_with_apex.htm. 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 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm and http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.htm. You could use a http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_field_sets.htm 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 http://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_field_sets.htm 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>
user4955 at Salesforce Visit the source
Other answers
To create a new template, go to Setup → (Administration Setup) Communication Templates → Email Templates and click New Template You then have 4 options: 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. 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 http://help.salesforce.com/ for more information. Also, some specific documentation on creating http://help.salesforce.com/HTViewHelpDoc?id=creating_custom_html_email_templates.htm&language=en_US.
amatorVitae
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 http://salesforce.stackexchange.com/questions/4303/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...).
eyescream
Related Q & A:
- How To Retrieve The Component Presentations Using DWT In Page Template Templating?Best solution by Tridion
- How do I check a type is of a specific template?Best solution by Stack Overflow
- How can I query Parse data by creation date with Swift?Best solution by Stack Overflow
- What is Fiat Creation?Best solution by Yahoo! Answers
- The creation of Nunavut.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.