+ Start a Discussion
ilaiilai 

Transfer lead email alert

Transferring Leads individually or via the enhanced List View allows the users to check Send Email Notification checkbox and indicate that an email be sent to the person who the Leads has been transferred to. However, if 10 Leads are transferred, the new owner will get 10 emails.  This is a SFDC system default. 

Below is the trigger, when user transfer 3 Lead records, the new owner is getting 3 emails still.

Would like to ask how to write the trigger in order to send only 1 email to the new owner instead.

 

 

trigger changeOwnerEmailAlert on Lead (after update) {

    List<Lead> changeOwnerLead = new List<Lead>();
    
    for(Lead le: Trigger.new) {
            changeOwnerLead.add(le);
        
            User leadOwner = [SELECT Name,Email FROM User WHERE Id=:le.OwnerId];

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {leadOwner.Email};
            mail.setToAddresses(toAddresses);
            mail.setSenderDisplayName('Salesforce Support');
            mail.setSubject('Lead(s) have been assigned to you');
            mail.setUseSignature(false);
           
            String msg = 'The following Lead(s) have been assigned to you';

            for(Lead l: Trigger.new) {      
                msg = msg + '<li>'+l.Name+' - '+l.Company+'</li>';        
            }      
            msg = msg + '</ol>';          

            mail.setHtmlBody(msg);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

   }
}