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
bhanu_prakashbhanu_prakash 

send email to user whenever lead is status is changed

hi Team,

send email to user whenever lead is status is changed. using trigger 
Abdul KhatriAbdul Khatri
same using workflow
pankul guptapankul gupta
Hi Bhanu,

Please find the code below for the same for sending an email to the Email mapped on the lead from trigger when the status get changed.
 
trigger leadStatusChange on Lead (after update) {
    
    List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
    for(Lead ld: trigger.new){
        if(ld.Status != trigger.oldMap.get(ld.Id).status){
            Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();
            sendTo.add(ld.Email);
            mail.setToAddresses(sendTo);
            
            mail.setReplyTo('pangupta@apttus.com');
            mail.setSenderDisplayName('Pankul Gupta');
            
            mail.setSubject('Lead Status Change');
            String body= 'Dear ' + ld.FirstName + ',' + 'The Status of the Lead has been changed to' + ld.Status;
            body += 'Email Body.';
          
            mail.setHtmlBody(body);
            
            mails.add(mail);
        }
    }
    
    if(mails.size() > 0){
        Messaging.sendEmail(mails);
    }

}

Please let me know if the abover snippet suffice youir requirement.

Regards,
Pankul