+ Start a Discussion
Sunny SSunny S 

Apex -Trigger to send email notification

Hi all,
Need help. I am still a novice with Development, hence would need someone to please help me with the below query.

Background: We are having a custom object as 'Survey', which is having around 10-12 fields (combination of Text and picklist field).

Use of the Object: The fields in this object 'Survey' are getting populated dynamically - through AMP Script from Marketing Cloud. As these surveys are sent to customers through Marketing Cloud emails, and whatever input the customer add on the Survey Cloud Page, it automatically gets recorded under same name fields in Sales Cloud object.

Requirement: We want to send an 'Email Notification' to the Manager, each time a survey gets recorded in the Sales Cloud Object 'Survey' object.
Also, the Notification Email should display the 'Customer Name' 'Email' &  'Answers to all the questions of the Survey'.

I am not too sure how to achieve this using Apex and Trigger ?
Can someone please help me with this and explain the steps in basic language.

Thanks in advance !
Best Answer chosen by Sunny S
Deepali KulshresthaDeepali Kulshrestha
Hi Sunny,

I've gone through your requirement and you can use the below code to send the email to survey:


trigger SendEmail on Survey__c (after insert) {
   
        set<id> allManager = new set<id>();
 
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails =
  new List<Messaging.SingleEmailMessage>();
 
  for (Survey__c surv : Trigger.new) {
    
    if (surv.Manager__c != null )
    {
          allManager.add(surv.Manager__c);
    }
         List<Manager__c> managerList = new List<Manager__c>([Select firstname,lastname,email,id,name,MobilePhone from Manager__c where id in :allManager]);
         List<Messaging.SingleEmailMessage> mailList =  new List<Messaging.SingleEmailMessage>();
         
         for(Manager__c man:managerList)
      {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
   
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(man.email);
      mail.setToAddresses(sendTo);
   
      // Step 3: Set who the email is sent from
      mail.setReplyTo('utkarsha.up10@gmail.com');
      mail.setSenderDisplayName('Utz patil');
   
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('puja.patil@aress.com');
      mail.setCcAddresses(ccTo);
 
      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + con.FirstName;
     
      mail.setHtmlBody(body);
   
      // Step 5. Add your email to the master list
      mailList.add(mail);
      
      }
        Messaging.sendEmail(mailList);
    }
  }
  // Step 6: Send all emails in the master list



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Somya TiwariSomya Tiwari

You can write a APEX Trigger for the same in After Update instance. For the same you can use the SingleEmailMessage class for the same. Head over to the link to learn more about the class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

Sunny SSunny S
Hi Somya, great thanks for your reply.

I have some basic understanding of using an After Update trigger and creating a Class, but I am really not too confident and sure about mapping/writing the "Survey fields and the recorded Answers" onto the Apex.
 
Deepali KulshresthaDeepali Kulshrestha
Hi Sunny,

I've gone through your requirement and you can use the below code to send the email to survey:


trigger SendEmail on Survey__c (after insert) {
   
        set<id> allManager = new set<id>();
 
  // Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails =
  new List<Messaging.SingleEmailMessage>();
 
  for (Survey__c surv : Trigger.new) {
    
    if (surv.Manager__c != null )
    {
          allManager.add(surv.Manager__c);
    }
         List<Manager__c> managerList = new List<Manager__c>([Select firstname,lastname,email,id,name,MobilePhone from Manager__c where id in :allManager]);
         List<Messaging.SingleEmailMessage> mailList =  new List<Messaging.SingleEmailMessage>();
         
         for(Manager__c man:managerList)
      {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
   
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(man.email);
      mail.setToAddresses(sendTo);
   
      // Step 3: Set who the email is sent from
      mail.setReplyTo('utkarsha.up10@gmail.com');
      mail.setSenderDisplayName('Utz patil');
   
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('puja.patil@aress.com');
      mail.setCcAddresses(ccTo);
 
      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + con.FirstName;
     
      mail.setHtmlBody(body);
   
      // Step 5. Add your email to the master list
      mailList.add(mail);
      
      }
        Messaging.sendEmail(mailList);
    }
  }
  // Step 6: Send all emails in the master list



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer
Sunny SSunny S
Great thanks Deepali !