banner



How To Use Letter Head In Visualforce Email Template

What are Visualforce email templates?

Visualforce Email Templates are basically a type of Salesforce email template that you can create when building classic email templates. Salesforce email templates are categorized into five different types, with slight differences in their use cases:

  • Text Email Template – This is the simplest type of Salesforce email template that you can implement. As the name implies, the email body will be a text only format, with only a simple subject and text body. But that doesn't mean that it is totally useless, as it still supports creating merge fields to pull data from recipient and source records.
  • HTML (using Classic Letterhead) Email Template – This is the most widely used email template among the available types of templates you can configure. It is technically a step-up from the Text version, as HTML email templates support the use of a Classic Email Letterhead. This letterhead is kind of a header, footer, and background template that you can use to apply your company's branding for your outgoing emails.
  • Custom (without using Classic Letterhead) Email Template – Custom email templates are the same as HTML email templates, but you can build the HTML email without a letterhead. A typical use case for the Custom email template is for admins and users to copy a whole HTML email template from an external source into Salesforce. It might get a bit complicated though because you need to have an understanding of HTML and CSS to be able to format the email correctly.
  • Visualforce Email Template – A VF email template is required to be used if you are planning to pull data from records that are related only to your source Salesforce object. An example would be to send details of an Opportunity record, with an added table containing data from the Products added to your opportunity. As the name implies though, you would need to have at least a basic grasp of Visualforce markup and a bit of Apex coding to be able to pull the data from your Salesforce records.
  • Lightning Email Templates – Lightning email template is your bet if you want your users to be able to easily create, use, and update email templates without going to setup. That is because they are created just like a normal Salesforce record, unlike in Classic where you need to have access to Setup before you can create or update the templates. It supports merge fields, enhanced letterheads, and arguably its best feature, folders and enhanced sharing. This feature allows you to create nested folders with custom sharing applicable at all levels. The only caveat with using lightning email templates is that it will be difficult to move them through different sandboxes and orgs since they are not supported by the metadata api.

Html email templates use cases

Html email templates in Salesforce are the bread and butter of both manual and automated emails. Both the classic and the lightning email template types support creating html email templates. The most common use case is to send automated email alerts for your customers at specific updates done to their Case or Opportunity records. You can add your brand's logo and colors using the letterhead feature to automatically add a branded header and footer. And like the text email templates, html versions also supports merge fields to automatically pull data

The main issue with html email templates is that it has limited access to related records. An example would be to show data from records 2 levels down or up in the relationship hierarchy. You can workaround the issue with records 2 levels up the hierarchy by creating formula fields to get the field values for you. However, formula fields cannot access the fields for child or grandchild records. You may also get a requirement to show data that is indirectly related to your main object, which is impossible to show using merge fields in either html or text email templates. This is where Visualforce email templates come in!

Visualforce email templates use cases

This particular template is a powerful and flexible solution to create email templates with complex features required. From experience, here are the typical Use-Cases of VF email templates:

  • For a Sales Cloud implementation, you can leverage a VF email template to send out an email to your partners containing information about their Opportunity, and the Products that have been added to each one of them in a table format.
  • An email containing a group of Contacts as participants of an event or meeting in a list format. You can send out a mass email to a Contacts list view that contains all the names of the contacts in that list, which pertains to all people that will be attending.
  • For a Case management solution, you can send out details of a parent Case that has been closed, together with the details of its child cases to give the customer a complete view of its resolution.

The possibilities are endless with Visualforce Email Templates, and you can also leverage the different Visualforce tags such as page blocks, data tables, and image blocks to fully customize your email.

Example code for a Visualforce email template

This is a sample code to showcase the features of a VF email template, utilizing use-case #1 for a Sales cloud implementation. As mentioned above, you will need to be knowledgeable in basic VF markup and apex code.

1. Create an Apex controller class

You will need to create an Apex class named OpportunityProductComponentCtrl to be able to pull data from the Opportunity Products of each Opportunities of the Account.

          public class OpportunityProductComponentCtrl {          private List<OpportunityLineItem> opptyProducts;     public String accId {get; set;}          public OpportunityProductComponentCtrl() {              }          public List<OpportunityLineItem> getOpportunityProducts() {         opptyProducts = [select Id, Opportunity.AccountId, Opportunity.Name, Product2.Name, UnitPrice, Quantity from OpportunityLineItem where Opportunity.AccountId =: accId];         return opptyProducts;     } }        

The List of OpportunityLineItems opptyProducts will be the container for all the products queried by the class.
The String accId will be the variable where we will insert the Id of the recipient's Account record. We will be populating this variable from the Visualforce Component we will create later.
The apex controller needs a constructor, but we will just leave it empty.
Finally, we create a method called getOpportunityProducts that returns a list of OpportunityLineItem when called. Inside the method is a SOQL query to get all opportunity products of Opportunities where the Account Id is equal to the accId that we will populate from the email template. We will put the query results into the opptyProducts List and then return it.

2. Create the Visualforce component

Next is to create the Visualforce component that will show the data we queried into the user interface. Let's name the component as OpportunityProductComponent

          <apex:component access="global" controller="OpportunityProductComponentCtrl">     <apex:attribute type="String" name="accountId" assignTo="{!accId}" description="Value to pass into the controller"/>     <apex:dataTable value="{!OpportunityProducts}" var="prod" id="theTable"                     rowClasses="odd,even" styleClass="tableClass">         <apex:facet name="header">Invoice</apex:facet>         <apex:column style="width:200px">             <apex:facet name="header">Product Name</apex:facet>             <apex:outputText value="{!prod.Product2.Name}"/>         </apex:column>                  <apex:column style="width:200px">             <apex:facet name="header">Price</apex:facet>             <apex:outputText value="{!prod.UnitPrice}"/>         </apex:column>                  <apex:column style="width:200px">             <apex:facet name="header">Description</apex:facet>             <apex:outputText value="{!prod.Quantity}"/>         </apex:column>     </apex:dataTable> </apex:component>        

The Apex class we created from #1 will be the component's controller.
The next tag, <apex:attribute> is used to pass a value to the accId variable of the controller, by using an attribute named accountId. Later, we will pass an Account Id from the email template to this accountId variable.
The next tags is to create a data table to hold the Products we queried from the Apex Class. You will notice that the method we used from the class is called getOpportunityProducts. In the dataTable, we only need to put OpportunityProducts to refer to the products. We leave the 'get' part of the method name, and the assign each iteration of Product to a variable called prod.
The facet tags are added to for the table header and each of the column's headers. Each column represents fields from the OpportunityLineItem list, with the contents to be displayed via an <apex:outputText> tag.

3. Create the Visualforce email template

Finally, we need to create the Visualforce Email Template. Go to Setup, type Classic Email Templates in the quick find search, and select the option. Click new and select Visualforce as the template type. Populate the Email Template Information including the Folder, Names, Description, and Subject. On the Visualforce Template Attributes, you need to add a temporary subject which we can revise later. Select Contact as the recipient type to send to a Contact, and then finally select Account as the Related To Type.

On the next step, you can replace the contents of the email body with the code below:

          <messaging:emailTemplate subject="Here are your Opportunity Details" recipientType="Contact" relatedToType="Account"> <messaging:htmlEmailBody >         <html>             <body>               <p>Hi {!recipient.Name},</p>             <p>Thank you for dealing with our Company</p>                          <p>Below are the products included on your Invoice:</p>                          <c:OpportunityProductComponent accountId="{!relatedTo.Id}"/>             <p>Please do not hesitate to contact us for any issues with your purchases.</p>                          <p>Cheers,</p>             <p>Thinky</p>             </body>         </html>     </messaging:htmlEmailBody> </messaging:emailTemplate>        

We use an <messaging:htmlEmailBody> tag to create an HTML email body and use the appropriate HTML tags. This is basic html markup, except for the middle part where we insert our component from #2 into our Visualforce Email Template
<c:OpportunityProductComponent accountId="{!relatedTo.Id}"/>
The code calls the component called OpportunityProductComponent which we created, and passes the {!relatedTo.Id} merge field, referring to the Account's Id into the accountId variable. Save the email template.
This completes the passing of data from Controller, to Component, and finally to the Email.

Putting it all together

Not that we are done with our coding, we can finally test our email template. Click the Send Test and Verify Merge Fields button to test the template and check if the data is indeed getting pulled. Select a Contact recipient, and then select an Account record as the related Record or Id.

Visualforce Email Sent with Product Details
Visualforce Email Sent with Product Details

Try it out on your own! You can use a Visualforce Email Template for any complex emailing requirement you have and utilize the power of Visualforce and Apex.

Final Thoughts

Aside from choosing the correct email template type for your exact requirement, you also need to consider more factors. These include concepts such as how you want to send the email and to whom.

You also need to implement a process to track emails in Salesforce using an out of the box method or an integration . Handling email replies using a case thread id in a Salesforce email is also important to note.

You will need to remember though that for every feature you enable, consider the business as a whole. Take note of compliance and government regulations to make sure the process will not impact operations.

How To Use Letter Head In Visualforce Email Template

Source: https://thinkoutcloud.info/visualforce-email-template-implementation-guide/

Posted by: stokeswouslacept.blogspot.com

0 Response to "How To Use Letter Head In Visualforce Email Template"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel