February 9, 2021

Email Notification to Contact after lead conversion in Salesforce

Sample Code:
trigger LeadTrigger on Lead ( after update ) {

    Set < Id > setContactIds = new Set < Id >();
   
    for ( Lead objLead : trigger.new ) {
       
        /* Getting Contact Ids from the Lead after conversion .
           ConvertedContactId in Lead contains the Contact Id to which the Lead is converted.
        */
        if ( objLead.IsConverted && objLead.IsConverted != trigger.oldMap.get( objLead.Id ).IsConverted )
            setContactIds.add( objLead.ConvertedContactId );
   
    }
   
    if ( setContactIds.size() > 0 ) {   
    
        List < String > toAddresses = new List < String >();
        List < Messaging.SingleEmailMessage > mails = new List < Messaging.SingleEmailMessage >();
        
        for ( Id conId : setContactIds ) {                       
            
            Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();                     
            mail.setTargetObjectId( conId );
            mail.setSaveAsActivity( false );
            mail.setReplyTo( 'info@infallibletechie.com' );
            mail.setSenderDisplayName( 'Salesforce Support' );
            mail.setSubject( 'Welcome to InfallibleTechie' );
            mail.setHtmlBody( 'Hello,<br/>A very warm welcome from our community!<br/><br/>Regards,<br/>Admin' );   
            mails.add( mail );
            
        }
        
        Messaging.sendEmail( mails );
   
    }

}

Output:


No comments:

Post a Comment