function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shruthi MN 88Shruthi MN 88 

Send email when the Lead Owner changes

Can you help me write a handler class for the below code
 

         4. Send email when the Lead Owner changes. Send email to the new lead owner and keep your respective Mentors in Cc of email.

( email body must have proper contain )


public class SendEmailHandler {
    List<Messaging.SingleEmailMessage> mails = 
  new List<Mesaging.SingleEmailMessage>();
    {
  for (Lead mylead : Trigger.new) {
    if (mylead.Email != null && mylead.FirstName != null) {
    
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      
      List<String> sendTo = new List<String>();
      sendTo.add(mylead.Email);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('abc@gmail.com');
      mail.setSenderDisplayName('Lead has been created');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('abc@gmail.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('Lead Owner has been changed');
      String body = 'Dear ' + mylead.FirstName + ', ';
      body += 'The Lead owner has been changed';
      body += 'Please take care of the lead';
      
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

}
AshwiniAshwini (Salesforce Developers) 
Hi Shruthi,
You can refer below code and make adjustments as per your requirement.
public class LeadOwnerChangeEmailHandler {

    public static void sendEmails(List<Lead> newLeads, Map<Id, Lead> oldLeadMap) {
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

        for (Lead newLead : newLeads) {
            // Check if the Lead Owner has changed
            if (newLead.OwnerId != oldLeadMap.get(newLead.Id).OwnerId) {
                if (newLead.Email != null && newLead.FirstName != null) {
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    
                    // Recipient
                    List<String> sendTo = new List<String>();
                    sendTo.add(newLead.Email);
                    mail.setToAddresses(sendTo);
                    
                    // Sender and CC
                    mail.setReplyTo('abc@gmail.com');
                    mail.setSenderDisplayName('Lead Owner Change Notification');
                    List<String> ccTo = new List<String>();
                    ccTo.add('abc@gmail.com'); // Add your mentors in CC
                    mail.setCcAddresses(ccTo);

                    // Email Subject and Body
                    mail.setSubject('Lead Owner has been changed');
                    String body = 'Dear ' + newLead.FirstName + ',\n\n';
                    body += 'The Lead owner has been changed.\n';
                    body += 'Please take care of the lead.\n';
                    mail.setPlainTextBody(body);
                     mails.add(mail);
                }
            }
        }
        
     
        if (!mails.isEmpty()) {
            Messaging.sendEmail(mails);
        }
    }
}

If this information helps, please mark the answer as best. Thank you
Shruthi MN 88Shruthi MN 88
I have written the below trigger . I am getting the attached error.
Can you help me execute it in the execuet anynomous window?

trigger SendEmilTrigger on Quote (after insert) {
Map<Id, Lead> oldLeadMap = Trigger.oldMap;

    SendEmailTriggerHandler.sendEmailOnLeadOwnerChange(Trigger.new, oldLeadMap);

}
User-added image
AshwiniAshwini (Salesforce Developers) 
Hi Shruthi,
In your trigger, you are trying to call a method "sendEmailOnLeadOwnerChange" from a class named SendEmailTriggerHandler, but in the provided code snippet, the class name is SendEmailHandler.
You should match the class name in your trigger with the actual class name.
Example:
trigger SendEmailTrigger on Quote (after insert) {
    classname.methodname(Trigger.new, Trigger.oldMap);
}
Replace class name and method name with acutal names.


In the Execute Anonymous window, you can insert the following code to insert a Quote record.
Quote newQuote = new Quote();
newQuote.Name = 'Test Quote';
// Set other required fields
insert newQuote;
If this information helps, please mark the answer as best. Thank you