Hey guys, today in this post we are going to learn about Apex Trigger to Send a Custom Visualforce Component Email Template when Record is Created on Custom Object in Salesforce.
Real time scenarios:- Write a trigger on Custom object (Registration__c) and Send a Custom Visualforce Component Email Template when Record is Created Condition is the Student Name should not be null.
Files we used in this post example
sendCustmTemp.apxt | Apex Class Trigger | It will be fire whenever a Record is Created Where Student name should not be null. |
recipientsController.apxc | Apex Class Controller | Create Apex controller to send email template through Apex Class Method |
emailNotifyTemp | Create Classic Email Templates | Create Classic Custom Email Templates. |
regEmailTempVfc.vfc | Visualforce Component | Create Visualforce Component. |
regTempController.apxc | Apex Class Controller | It is call on Visualforce Component. |
Final Output
Other related post that would you like to learn…
Create Apex Class Trigger
Step 1:- Create Apex Class Trigger : sendCustmTemp.apxt
From Developer Console >> File >> New >> Apex Class
sendCustmTemp.apxt [Apex Class Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
trigger sendCustmTemp on Registration__c (before insert, before update, after insert, after update) { if(trigger.isAfter && (Trigger.isInsert)){ for(Registration__c myEmail:trigger.new){ Registration__c stuReg = [select id,Name, RegNewStudent__r.Name From Registration__c Where id=:myEmail.id]; system.debug('myEmai444'+ stuReg.RegNewStudent__r.Name); if(stuReg.RegNewStudent__r.Name != null){ recipientsController.sendEmail(stuReg.id); } } } } |
Create Apex Class controller
Step 2:- Create Apex Class : recipientsController.apxc
From Developer Console >> File >> New >> Apex Class
recipientsController.apxc [Apex Class Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public class recipientsController { @AuraEnabled public static void sendEmail(Id RegId){ //String toAddress, String Subject, String notes, String recId Registration__c newRegistration = new Registration__c(); newRegistration = [Select Id, Name, RegNewStudent__r.Name, RegCourse__r.Name, DateOfRegistration__c,sendSubjectLine__c, SendTemplateNote__c From Registration__c where Id=: RegId]; Double unique= math.random()* 10000 ; String uniquestr = unique.format().remove(',').substring(0,4) ; string mil = UserInfo.getUserEmail(); string nm = 'test lastname' + uniquestr; Contact tempContact = new Contact(email = mil, firstName = 'test' +uniquestr, lastName = nm); insert tempContact; //Fetching Email template EmailTemplate templateId = [Select id from EmailTemplate where Name LIKE 'emailNotifyTemp%' limit 1]; system.debug('templateId:: '+templateId); // string param1 ='<html>abcd</html>'; List<string> emailstr = new List<String>(); //lstIdTO.add(UserInfo.getUserEmail()); //emailstr.add('w3webmart@gmail.com'); emailstr.add('w3webnet@gmail.com'); list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>(); Messaging.SingleEmailMessage mails = new Messaging.SingleEmailMessage(); mails.setTemplateID(templateId.Id); mails.setToaddresses(emailstr); mails.setTargetObjectId(tempContact.id); system.debug('Mails'+mails); mail.add(mails); //system.debug('mail#105 >>'+mail); Messaging.sendEmail(mail); //system.debug('Ten Value >>'+mail); //delete tempContact; } } |
Create Classic Email Templates
Step 3:- Create Classic Email Templates : emailNotifyTemp
File >> New >> Classic Email Templates >> emailNotifyTemp
emailNotifyTemp [Classic Email Templates File]
1 2 3 4 5 6 7 8 9 |
<messaging:emailTemplate subject="Registration Notification" recipientType="User" relatedToType="Registration__c"> <messaging:htmlEmailBody > <p>w3web.net is the place where you can learn step-by-step about Blog, WordPress, Salesforce Lightning Component, Lightning Web Component (LWC), Visualforce, Technical of Computer Application, Salesforce Plugin, JavaScript, Jquery, CSS, Computer & Accessories, Software Configuration, Customization, Development and much more…</p><br/><br/> <c:regEmailTempVfc /> </messaging:htmlEmailBody> </messaging:emailTemplate> |
Create Visualforce Component
Step 4:- Create Visualforce Component : regEmailTempVfc.vfc
File >> New >> Visualforce Component >> regEmailTempVfc.vfc
regEmailTempVfc.vfc [Lightning Component File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<apex:component controller="regTempController" access="global"> <table border="1" cellpadding="5" cellspacing="5" style="border-collapse:collapse;"> <thead> <tr> <td colspan="3"><span style="display:none;">Custom Label Img:-</span> <img src="{!$Label.w3webLabel}" alt="" align="left" width="134" height="60"/></td> <td colspan="3"><span style="display:none;">Documet Img:-</span> <img src="https://vijay3327-dev-ed--c.ap4.content.force.com/servlet/servlet.ImageServer?id=0156F00000IvXxw&oid=00D6F000000GGe0&lastMod=1572421120000" width="50" height="40"/></td> </tr> <tr style="background:rgb(0, 112, 210); color:#fff;"> <th>Reg No.</th> <th>Student Name</th> <th>Course Name</th> <th>Date Of Registration</th> <th>Subject Line</th> <th>Template Note</th> </tr> </thead> <tbody> <apex:repeat value="{!RegList}" var="regVar"> <tr> <td>{!regVar.Name}</td> <td>{!regVar.RegNewStudent__r.Name}</td> <td>{!regVar.RegCourse__r.Name}</td> <td> <apex:outputText value="{0,date,MM/dd/yyyy}"> <apex:param value="{!regVar.DateOfRegistration__c}" /> </apex:outputText> </td> <td>{!regVar.sendSubjectLine__c}</td> <td>{!regVar.SendTemplateNote__c}</td> </tr> </apex:repeat> </tbody> </table> </apex:component> |
Create Visualforce Component Apex Class Controller
Step 5:- Create Apex Class : regTempController.apxc
From Developer Console >> File >> New >> Apex Class
regTempController.apxc [Apex Class Controller]
1 2 3 4 5 6 7 8 9 10 |
public class regTempController { public static List<Registration__c> getRegList(){ List<Registration__c> newRegistration; newRegistration = [Select Id, Name, RegNewStudent__r.Name, RegCourse__r.Name, DateOfRegistration__c,sendSubjectLine__c, SendTemplateNote__c From Registration__c ]; return newRegistration; } } |
➡
TechW3web:- Apex Trigger to Send a Custom Email Template When Record is Created on Custom Object in Salesforce
Other post that would you like to learn in LWC
I really enjoyed reading your memoirs. I am an avid Star Wars fan, as well as an artist, and every once in a while I look for good articles to read on some of the things that interest me. All I had to do was type in Darth Vader and there you where. Keep up the good work, and maybe even check out some of my Star Wars art that I have published on my art profile sometime. satta Matka