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
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Apex Class Trigger
Step 1:- Create Apex Class Trigger : sendCustmTemp.apxt
From Developer Console >> File >> New >> Apex Class
sendCustmTemp.apxt [Apex Class Controller]
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]
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]
<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 and 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]
<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]
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;
}
}
Further post that would you like to learn in Salesforce
What is Apex class in Salesforce?
An Apex class is a template or blueprint from which Apex objects are created. Classes consist of other classes, user-defined methods, variables, exception types, and static initialization code.
What do Apex classes do?
An apex class is a blueprint or template from which objects are created. An object is the instance of a class. Apex class detail page. In apex, you can define an outer class also called top-level class, and you can also define classes within an outer class called inner classes.
What language is Apex based on?
Apex is based on familiar Java idioms, such as variable and expression syntax, block and conditional statement syntax, loop syntax, object, and array notation.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |