Salesforce.com-Tips and Tricks
Greytrix is one of the oldest Sage Development Partner of two decades for Sage product lines and Reseller of Sage ERP and CRM. Greytrix has been awarded "Sage Partner of the Year" numerous times both as developers and resellers. In these blogs, Greytrix will endeavour to share its knowledge with regards to implementation, training, customisation, components and technology and help users to understand in depth techno – functional aspects of Salesforce.com To leverage the Greytrix advantage write to us at salesforce@greytrix.com
Follow us on LinkedIn Follow us on Twitter Follow us on Facebook Follow us on YouTube
Skip to content
  • Home

How to Use Email Template in Apex Code

By Greytrix | September 22, 2021
0 Comment

In this blog, we will discuss how to How to Use an Email Template in 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 & you can also modify it based on your need.

Steps to Create a New Email template are as follows:

  1. Go to Setup-> search ‘template’ -> choose ‘Classic Email Templates’-> click on ‘New Template’ button.
  2. Select Visualforce option for ‘type of email template’.
  3. Enter template name, keep the other defaults. For this example, we use ‘Test Account Alert Email’.
  4. Enter Email Subject text.
  5. Choose Recipient Type Object, for this example we choose Contact object.
  6. Choose ‘Related To Type’ as Account object for this example.
  7. Save.
  8. Choose ‘Edit Template’ button to change the content of the email body. We could use the merge fields in the content and in subject

Example of Visualforce template:

<messaging:emailTemplate recipientType="Contact"
= relatedToType="Account" subject="Test Email" replyTo="{!recipient.Email}">
   <messaging:htmlEmailBody >
    <html>
        <body>
           <p> Dear {!relatedTo.Name}, </p>
           <p></p>
           <p> It is an Test Eamil </p>
           <p></p>
         </body>
    </html>
  </messaging:htmlEmailBody>
</messaging:emailTemplate>

Apex logic to send email using above created template:

Public static void sendNotification(Account accountEmail)
{
   Messaging.SingleEmailMessage message = new        
   Messaging.SingleEmailMessage();
   message.setTargetObjectId(contact.id);
   message.setSenderDisplayName(‘Company Support’);
   message.setReplyTo(‘no-reply@company.com’);
   message.setUseSignature(false);
   message.setBccSender(false);
   message.setSaveAsActivity(false);
   EmailTemplate emailTemplate =[Select, Id,Subject,Description,
                                 HtmlValue,DeveloperName,Body
                                 from EmailTemplate where name =
                                 ‘Test Account Alert Email’];
   //Get templete id for set the templete.

   message.setTemplateID(emailTemplate.Id);
   message.setWhatId(account.Id); //This is important for the merge
                                    fields in template to work
   message.toAddresses = new String[] { contact.email};

   Messaging.SingleEmailMessage[] messages = new   
   List<Messaging.SingleEmailMessage> {message};

   Messaging.SendEmailResult[] results =
   Messaging.sendEmail(messages);
 
 if (results[0].success)
 {
   System.debug(‘The email was sent successfully.’);
 }
 else
 {
   System.debug(‘The email failed to send: ‘ +
   results[0].errors[0].message);
 }
}

We hope using the information; you are able to create and use an email template.

We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, don’t hesitate to get in touch with us at salesforce@greytrix.com.

About Us
Greytrix – 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.

Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support to business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.

The GUMU™ 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.

For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts

  • How to Avoid Null Pointer Exception By Using Safe Navigator Operator (?.)
  • How to create a dynamic multi-filter object in Salesforce-Part I
  • Clone Record of Any Object using Flows in Salesforce

Category: Salesforce Services Tags: Classsic Email templete, Email Templete, EmailTemplete, Send email from Apex, Template
Post navigation
← GUMU™ Features – Bulk Account Update The @track Decorator Is No Longer Required for Lightning Web Component but with one Exception →

Greytrix Websites and Blogs

  • Greytrix
  • GUMU™ Cloud
  • Greytrix Africa
  • Greytrix Australia
  • Sage X3 – Tips, Tricks and Components
  • Sage 100 & 500 ERP Tips and Tricks
  • Sage 300 – Tips, Tricks and Components
  • Sage CRM – Tips, Tricks and Components
  • Sage Business Cloud – Tips, Tricks and Components

GUMU™ Salesforce Sage Integration

https://www.youtube.com/watch?v=X9Dg4uwGT-0

Categories

  • GUMU™ Features
  • GUMU™ Salesforce
  • GUMU™ Tricks & Tips
  • Salesforce Services
  • Uncategorized
sage enterprise management services

Recent Posts

  • Use Of Fault Connector in Flow Builder
  • How does GUMU™ for Salesforce – Sage Intacct Integration help Non-Profits to drive efficiency & growth?
  • GUMU™ – A Game Changer in Salesforce – Sage ERP Integration
  • How to Get List of Apex Class by using ApextypeImplementor
  • How to Promote AR-Invoice from Salesforce to Sage 300 Cloud Using Salesforce Classic Version

Popular blogs

  • Thinking of enhancing your business processes? Get the best ERP – CRM connector on Salesforce AppExchange!
  • GUMU™ for Salesforce – Sage ERP Integration
  • Leverage real-time enterprise data for better insights of your Customers with Salesforce – Sage 300 integration
  • GUMU™ Integration for Salesforce with Sage 300

Archives

Awards

Greytrix Top ISV Partner
Greytrix Revenue Growth Enterprise Business
Greytrix Africa Highest Revenue
Greytrix Prtner of the Year
Greytrix Partner Award
Copyright 2022
Salesforce.com-Tips and Tricks
Iconic One Theme | Powered by Wordpress