Home Salesforce Send Email Using Email Template and Apex

Send Email Using Email Template and Apex

by Dhanik Lal Sahni

Send Email Using Email Template and Apex

Sending Email is one of some important task in salesforce application like sending any report, send task information etc. We have below options to send email in salesforce.

  • Use Email Template and SingleEmailMessage class
  • Create VF Page and Send it using Apex

Let us see first approach in this post. For sending email with attachment below steps are required

  1. Create Email Template
  2. Send Email using Apex
  3. Create Lightning Action component
  4. Create Lightning Action on Object
  5. Add button on Page layout

We can send any report as attachment also. Let us see step by step information to send email using Email Template.

  1. Create Email Template

Email templates can be used to increase productivity and ensure consistent messaging. Email templates with merge fields let us quickly send emails that include field data from Salesforce records. We have used account record to create template.

Open email template page by navigating Setup->Classic Email Template->New Template->Visual Force

Provide email template information

Edit Template to create decline letter. This letter will be sent as email so Different HTML body and Email attachment will be provided.

Create email body based on your requirement.  After saving email body it will show HTML preview.

 

2. Send Email using Apex

Let us create an Apex method which will send email based on current record and above template id. Messaging.SingleEmailMessage is used to send single email message to respective user.

@AuraEnabled
public class CreditCardController {
        public void sendEmail(string acctid)
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            mail.setTargetObjectId(UserInfo.getUserId()); 
            //Change with Template ID
            mail.setTemplateId('00X7F000000EiTl'); 
            
            //Record ID for which letter will be generated
            mail.setWhatId(acctid); 
            mail.setBccSender(false); 
            mail.setUseSignature(false); 
            mail.setSenderDisplayName('Card Division'); 
            mail.setSaveAsActivity(false); 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
}

3. Create Lightning Action component

Button can be added to classic page layout and Lightning layout to send email. Let us create a lightning component which will work as quick action button. We will add this on our layout.

<aura:component controller="CreditCardController"
                implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickActionWithoutHeader" >
    <aura:attribute name="recordId" type="Id" />
    <aura:handler name="init" value="{!this}" action="{!c.apexExecute}"/>
</aura:component>
({
        apexExecute : function(component, event, helper) {
            //Call Your Apex Controller Method.
            var action = component.get("c.sendEmail");
            action.setParams({
                'acctid': ''+component.get('v.recordId')+''
            });

            action.setCallback(this, function(response) {
                var state = response.getState();
                    if (state === "SUCCESS") {
                        //after code
                        response.getReturnValue();

                        $A.get("e.force:closeQuickAction").fire();
                    } 
            });
            
            $A.enqueueAction(action);
	}
})

4. Create Lightning Action on Object

Create one Lightning action button on account page.  Select lightning component which we have created above.5. Add button on Page layout

You may also like

7 comments

Muni Prasad May 4, 2020 - 3:27 pm

Thats a nice tip. But i wanted to know, will this also work for custom objects?

Reply
Dhanik Lal Sahni May 4, 2020 - 9:42 pm

Yes Muni. It will work for custom object also.

Thank You,
Dhanik

Reply
Apex Send Email » LoginCast.Com May 22, 2021 - 2:28 am

[…] 7. Send Email Using Email Template and Apex | | SalesforceCodex […]

Reply
Gaurav August 11, 2021 - 4:06 pm

Sir its not working for my custom object please help for me.

Reply
Dhanik Lal Sahni August 12, 2021 - 9:56 pm

Hello Gaurav,

What is not working. Please put complete detail, what error you are getting.

Thank You,
Dhanik

Reply
Schedule Email alerts based on Business Hours in Salesforce September 2, 2021 - 12:12 pm

[…] Send Email Using Email Template and Apex […]

Reply
Apex Send Email • 3Z Acdemy October 20, 2021 - 1:49 pm

[…] 7. Send Email Using Email Template and Apex | | SalesforceCodex […]

Reply

Leave a Comment