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
PhoenixRising12PhoenixRising12 

Apex Trigger and Trigger Helper Class isn't firing

I have an Apex Trigger that's not firing. This Trigger is supposed to call a helper class which then merges Leads into existing accounts if the email already exists in the Salesforce org. I have the trigger and Class here, what can I change to make it work properly without errors? 

Trigger: 

trigger DuplicateMergeTrigger on Lead (after Insert) {
    
    if (Trigger.isInsert){
        
        Set<Id> LeadIds1 = new Set<Id>();


    for (Lead myLead : Trigger.New){
        
        
        
        if(myLead.Isconverted == FALSE && myLead.College_Corps__c == TRUE && myLead.Status == 'New'){
            
            
LeadIds1.add(myLead.Id);
            
        }
        
        
        
        if(LeadIds1.size() > 0){
            
     LeadConversionUtils.ConvertLeads(Trigger.new); 
            
        }
        
        
        
    }    
        
        
    }
   
}

Helper Class

public class LeadConversionUtils {

    public static void ConvertLeads(List<Lead> leadIds){
        
        
     
       LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
        
        List<Lead> LeadList = new List<Lead>();
        
        LeadList = [SELECT Id, Email FROM Lead WHERE IsConverted = FALSE];
        
        
       List<String> LeadEmails = new List<String>();
        
        for (Lead VarL : LeadList){
            
            if(VarL.Email != NULL){
                
                LeadEmails.add(VarL.Email);
                
            }
             
            
        }
        
      List<String> AccountEmails = new List<String>();
      List<Id> AccountIds2 = new List<Id>();
        
        for(Account VarA : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadEmails]){
            
            
            AccountEmails.add(VarA.PersonEmail);
            AccountIds2.Add(VarA.Id);
                        
        }
        
        if(AccountIds2.size() > 0){
            
             Id AccountId = AccountIds2[0];
            
            
            for (lead MyLead : leadList){
            
            
            if(myLead.Email != NULL && !AccountEmails.contains(myLead.Email)){
                
                
                Database.LeadConvert lc = new database.LeadConvert();
                lc.setLeadId(MyLead.Id);
                lc.setAccountId(AccountId);
                lc.setConvertedStatus(convertedStatus.MasterLabel);
                lc.setDoNotCreateOpportunity(TRUE);
        
              
            }

            
        }
            
        }
     
        
    }
   
}
Best Answer chosen by PhoenixRising12
Maharajan CMaharajan C
Hi  PhoenixRising,

The below line is missing in your handler class: 

Database.LeadConvertResult lcr = Database.convertLead(lc);

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_dml_convertLead.htm

Updated helper Class:

 
public class LeadConversionUtils {
    
    public static void ConvertLeads(List<Lead> leadIds){
        LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
        List<Lead> LeadList = new List<Lead>();
        LeadList = [SELECT Id, Email FROM Lead WHERE IsConverted = FALSE];        
        List<String> LeadEmails = new List<String>();
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

        for (Lead VarL : LeadList){
            if(VarL.Email != NULL){
                LeadEmails.add(VarL.Email);
            }
        }
        
        List<String> AccountEmails = new List<String>();
        List<Id> AccountIds2 = new List<Id>();
        
        for(Account VarA : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadEmails]){
            AccountEmails.add(VarA.PersonEmail);
            AccountIds2.Add(VarA.Id);
        }
        
        if(AccountIds2.size() > 0){
            Id AccountId = AccountIds2[0];
            for (lead MyLead : leadList){
                if(myLead.Email != NULL && !AccountEmails.contains(myLead.Email)){
                    Database.LeadConvert lc = new database.LeadConvert();
                    lc.setLeadId(MyLead.Id);
                    lc.setAccountId(AccountId);
                    lc.setConvertedStatus(convertedStatus.MasterLabel);
                    lc.setDoNotCreateOpportunity(TRUE);
                    leadConverts.add(lc);
                }
            }
        }     

       if (!leadConverts.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
            System.debug(+'result'+lcr);
        }   
    }
}


Thanks,
Maharajan.C

All Answers

VinayVinay (Salesforce Developers) 
Your trigger and class seem to be ok, can you try checking debug log to see if component is being invoked and that should give more details, also kindly re-test wtih required conditions on the record.

Thanks,
Maharajan CMaharajan C
Hi  PhoenixRising,

The below line is missing in your handler class: 

Database.LeadConvertResult lcr = Database.convertLead(lc);

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_dml_convertLead.htm

Updated helper Class:

 
public class LeadConversionUtils {
    
    public static void ConvertLeads(List<Lead> leadIds){
        LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
        List<Lead> LeadList = new List<Lead>();
        LeadList = [SELECT Id, Email FROM Lead WHERE IsConverted = FALSE];        
        List<String> LeadEmails = new List<String>();
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

        for (Lead VarL : LeadList){
            if(VarL.Email != NULL){
                LeadEmails.add(VarL.Email);
            }
        }
        
        List<String> AccountEmails = new List<String>();
        List<Id> AccountIds2 = new List<Id>();
        
        for(Account VarA : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadEmails]){
            AccountEmails.add(VarA.PersonEmail);
            AccountIds2.Add(VarA.Id);
        }
        
        if(AccountIds2.size() > 0){
            Id AccountId = AccountIds2[0];
            for (lead MyLead : leadList){
                if(myLead.Email != NULL && !AccountEmails.contains(myLead.Email)){
                    Database.LeadConvert lc = new database.LeadConvert();
                    lc.setLeadId(MyLead.Id);
                    lc.setAccountId(AccountId);
                    lc.setConvertedStatus(convertedStatus.MasterLabel);
                    lc.setDoNotCreateOpportunity(TRUE);
                    leadConverts.add(lc);
                }
            }
        }     

       if (!leadConverts.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
            System.debug(+'result'+lcr);
        }   
    }
}


Thanks,
Maharajan.C
This was selected as the best answer
PhoenixRising12PhoenixRising12
Thank you so much Maharajan! Gosh I feel silly now, the answer was right in front of me the whole time. Greatly appreciate the help!