{"id":6340,"date":"2024-02-22T05:24:00","date_gmt":"2024-02-22T05:24:00","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6340"},"modified":"2025-06-10T05:55:03","modified_gmt":"2025-06-10T05:55:03","slug":"how-to-use-email-template-in-apex-code","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2024\/02\/22\/how-to-use-email-template-in-apex-code\/","title":{"rendered":"How to Use Email Template in Apex Code"},"content":{"rendered":"\n<p>With this blog, we&#8217;ll look at how to use an email template with Apex Code.<\/p>\n\n\n\n<p>An email template is an HTML file that you use to build an email body. A good template will let you copy and paste content directly into the file. You can use the same template again and again &amp; you can also modify it based on your need.<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-a5e3bd97379b2f53e0fd76359ef18a36\"><strong>Steps to Create a New Email template are as follows:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to Setup-&gt; search \u2018template\u2019 -&gt; choose \u2018Classic Email Templates\u2019-&gt; click on \u2018New Template\u2019 button.<\/li>\n\n\n\n<li>Select Visualforce option for \u2018type of email template\u2019.<\/li>\n\n\n\n<li>Enter template name, keep the other defaults. For this example, we use \u2018Test Account Alert Email\u2019.<\/li>\n\n\n\n<li>Enter Email Subject text.<\/li>\n\n\n\n<li>Choose Recipient Type Object, for this example we choose Contact object.<\/li>\n\n\n\n<li>Choose \u2018Related To Type\u2019 as Account object for this example.<\/li>\n\n\n\n<li>Save.<\/li>\n\n\n\n<li>Choose \u2018Edit Template\u2019 button to change the content of the email body. We could use the merge fields in the content and in subject<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Example of Visualforce template:<\/em><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;messaging:emailTemplate recipientType=\"Contact\"\n= relatedToType=\"Account\" subject=\"Test Email\" replyTo=\"{!recipient.Email}\"&gt;\n   &lt;messaging:htmlEmailBody &gt;\n    &lt;html&gt;\n        &lt;body&gt;\n           &lt;p&gt; Dear {!relatedTo.Name}, &lt;\/p&gt;\n           &lt;p&gt;&lt;\/p&gt;\n           &lt;p&gt; It is an Test Eamil &lt;\/p&gt;\n           &lt;p&gt;&lt;\/p&gt;\n         &lt;\/body&gt;\n    &lt;\/html&gt;\n  &lt;\/messaging:htmlEmailBody&gt;\n&lt;\/messaging:emailTemplate&gt;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Apex logic to send email using above created template:<\/em><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Public static void sendNotification(Account accountEmail)\n{\n   Messaging.SingleEmailMessage message = new        \n   Messaging.SingleEmailMessage();\n   message.setTargetObjectId(contact.id);\n   message.setSenderDisplayName(\u2018Company Support\u2019);\n   message.setReplyTo(\u2018no-reply@company.com\u2019);\n   message.setUseSignature(false);\n   message.setBccSender(false);\n   message.setSaveAsActivity(false);\n   EmailTemplate emailTemplate =&#91;Select, Id,Subject,Description,\n                                 HtmlValue,DeveloperName,Body\n                                 from EmailTemplate where name =\n                                 \u2018Test Account Alert Email\u2019];\n   \/\/Get templete id for set the templete.\n\n   message.setTemplateID(emailTemplate.Id);\n   message.setWhatId(account.Id); \/\/This is important for the merge\n                                    fields in template to work\n   message.toAddresses = new String&#91;] { contact.email};\n\n   Messaging.SingleEmailMessage&#91;] messages = new   \n   List&lt;Messaging.SingleEmailMessage&gt; {message};\n\n   Messaging.SendEmailResult&#91;] results =\n   Messaging.sendEmail(messages);\n \n if (results&#91;0].success)\n {\n   System.debug(\u2018The email was sent successfully.\u2019);\n }\n else\n {\n   System.debug(\u2018The email failed to send: \u2018 +\n   results&#91;0].errors&#91;0].message);\n }\n}<\/code><\/pre>\n\n\n\n<p>We hope using the information; you are able to create and use an email template. <\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, don&#8217;t hesitate to get in touch with us at <a href=\"mailto:na.sales@greytrix.com\">na.sales@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/14\/how-to-avoid-null-pointer-exception-by-using-safe-navigator-operator\/\">How to Avoid Null Pointer Exception By Using Safe Navigator Operator (?.)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/17\/how-to-create-a-dynamic-multi-filter-object-in-salesforce-part-i\/\">How to create a dynamic multi-filter object in Salesforce-Part I<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/16\/clone-record-of-any-object-using-flows-in-salesforce\/\">Clone Record of Any Object using Flows in Salesforce<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>With this blog, we&#8217;ll look at how to use an email template with Apex Code. An email template is an HTML file that you use to build an email body. A good template will let you copy and paste content directly into the file. You can use the same template again and again &amp; you\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2024\/02\/22\/how-to-use-email-template-in-apex-code\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1203,1202,1201,595,486],"class_list":["post-6340","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-classsic-email-templete","tag-email-templete","tag-emailtemplete","tag-send-email-from-apex","tag-template"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6340"}],"version-history":[{"count":5,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6340\/revisions"}],"predecessor-version":[{"id":10858,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6340\/revisions\/10858"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}