Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apex Trigger to Send a Custom Visualforce Component Email Template when Record is Created on Custom Object in Salesforce #7

Open
vijayk3327 opened this issue Aug 27, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@vijayk3327
Copy link
Owner

vijayk3327 commented Aug 27, 2023

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.

👉 To get source code live demo link, Click Here.

Create Apex Class Trigger
Step 1:- Create Apex Class Trigger : sendCustmTemp.apxt

`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

`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

`<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

`<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

` 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;
}

}`

👉 To get source code live demo link, Click Here.

@vijayk3327 vijayk3327 added documentation Improvements or additions to documentation question Further information is requested labels Aug 27, 2023
@vijayk3327 vijayk3327 self-assigned this Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: No status
Development

No branches or pull requests

1 participant