0

I have a custom object managers__c which has a lookup field Partnership_managers__c with User. when the lead is converted into an account, all the partnership managers should be notified with the converted contact's details via mail. I have tried email alert with process builder but it's not working out in this case as the recipients would then have to be added manually which is not the right approach. Can someone please explain to me how to achieve this? Thanks already!!

 trigger LeadConvertNotification on Contact (After insert) {
    List<Managers__c> managersList = new List<Portfolio_Managers__c>();
         managersList =[Select id,Name,Partnerships_Manager__r.email from Managers__c limit 100];
        for(Contact newItem : trigger.new) {
            
        Contact con = [select id,email,firstName,lastname,accountId from Contact WHERE Id=:newItem.Id]; 
            
        Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();

         List<String> toAddresses = new List<String>();
        for(Managers__c man : managersList){
       toAddresses.add(man.Partnerships_Manager__r.email);
            
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('abc@def.com');
        mail.setSenderDisplayName('Salesforce Support');
        mail.setSubject('New Community user is Created : ');
        mail.setHtmlBody('Your community user: has been created');     
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
         }
        }}
2
  • You can use a trigger for this. – sfdcfox Jan 7 at 14:09
  • I have updated my trigger but hitting error, can you please correct me what i am missing here – MikeT Jan 7 at 14:44
0

Mike, Can you try the below 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 >();
        Set < Id > setManagerIds = new Set < Id >();
        
        for ( Managers__c man : [ SELECT Id, Partnerships_Manager__c, Partnerships_Manager__r.email FROM Managers__c LIMIT 100 ] ) {
            
            setManagerIds.add( man.Partnerships_Manager__c );
            toAddresses.add(man.Partnerships_Manager__r.email);
            
        }
        
        for ( Contact objCon : [ SELECT Id, Name FROM Contact WHERE Id IN: setContactIds ] ) {
            
            for ( Id usrId : setManagerIds ) {
                
                Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();                     
                mail.setTargetObjectId( usrId );
                mail.setSaveAsActivity( false );
                mail.setReplyTo( 'abc@def.com' );
                mail.setSenderDisplayName( 'Salesforce Support' );
                mail.setSubject( 'New Community user is Created : ' + objCon.Name );
                mail.setHtmlBody( 'Your community user: has been created' );   
                mails.add( mail );
                
            }
            
        }
        
        Messaging.sendEmail( mails );
   
    }

}

I have developed the trigger on Lead so that the email notification will be sent if the contact was created as part of Lead Conversion. I am also using setTargetObject() to avoid single email message governor limit.

Please adjust the code to match your business requirement.

Let me know if you have additional questions.

1
  • -1 for being mostly a code dump. The explanation of why it works and what the issues are with the original code are important for helping people learn how to not make the same mistakes in the future. – Derek F Jan 19 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.