The concepts of email templates is known to every salesforce developer and admin. We can create text template to send birthday wishes, or monthly summary, etc. to various people. However these templates are restricted while using those templates. We cannot attach any object that we want and use all it’s fields. For this purpose we have a Visualforce email template in salesforce. This type of template can be related to any object and then while sending the email from APEX we can simply attach the id of the object whose data we want in this template.
Example
To create a new visualforce template first go to Setup|Communication Templates|Email Templates and click on New Template. Select Visualforce and click next, enter the required data and click save. For this example I’ll be creating a new product alert template. Let us name it New Product Alert. Let email Subject be New Product Added. Recipient Type will be User. Related To type is Product 2. After Saving the template go to edit template and paste the following code there:
<messaging:emailTemplate subject="New Product Added" recipientType="User" relatedToType="Product2"> <messaging:htmlEmailBody > Hello {!recipient.Firstname},<br/><br/> A new product is created.<br/> Product Name: {!relatedTo.name}<br/> Product Code: {!relatedTo.productCode}<br/> Product Active: {!relatedTo.IsActive}<br/> Thank You, </messaging:htmlEmailBody> </messaging:emailTemplate>
As you can see here we have accessed the objects User and product2 in this template indirectly referencing them with recipient and relatedto.
Using the template
We can write a simple trigger to use this template every time a product is created. The code of the trigger is as follows:
trigger SendNotifyEmail on Product2 (after insert) { list prolist = trigger.new; List mails = new List(); for(product2 pro: prolist){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); Profile sys_adm_profile = [SELECT Id FROM Profile WHERE Name = 'System Administrator']; List sys_adm = [SELECT id, Email FROM User WHERE ProfileId = :sys_adm_profile.id]; list sendTo = new list(); for(user u:sys_adm){ sendTo.add(u.email); } mail.setToAddresses(sendTo); mail.setSenderDisplayName(Userinfo.getname()); mail.setTargetObjectId(sys_adm[0].id); mail.setWhatId(pro.Id); mail.setSaveAsActivity(false); mail.setTemplateId('00X6F000001GnyU'); mails.add(mail); } try{ Messaging.sendEmail(mails); }catch(Exception e){ system.debug(e); } }
Like you can see this is a normal trigger which will send email to all the system admin every time a new product is created. The setTemplateId methods is provided the id of the template that we are going to use for this email message.
Now every time a new product is created the system admins will receive an email about it.
Support
That’s all about using Visualforce Templates, for any further queries feel free to contact us at:
https://wedgecommerce.com/contact-us/
Or let us know your views on how to make this code better, in comments section below.