• PhoenixRising12
  • NEWBIE
  • 20 Points
  • Member since 2022
  • Salesforce Developer

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 2
    Replies
I have a Trigger Helper Class which I need a Test Class for. What would be the best way to approach this? 
 
public class DuplicateMergeTriggerHandler {
    
    public static void MergeHandler(Set<Id> leadIds) {
	
        List<String> LeadListEmails = new List<String>();
        List<String> LeadNames = new List<String>();
        List<String> LeadPhones = new List<String>();
        
        List<Lead> myleads = [SELECT Id, Name, Email, Phone FROM Lead WHERE Id IN :leadIds];
        
        for (Lead L1 : myleads) {
           LeadListEmails.add(L1.Email);
        }
		
        for (Lead L2 : myleads){
            LeadNames.add(L2.Name);
        }
        
        for (Lead L3 : myleads){
            
            LeadPhones.add(L3.Phone);
        }

        Map<String,Account> mapAccountsEmail = new Map<String,Account>();
         
        for (Account A : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadListEmails]){    
            mapAccountsEmail.put(A.PersonEmail, A);   
        }
        
        Map<String,Account> mapAccountsName = new Map<String,Account>();
        
        for (Account A1 : [SELECT Id, Name FROM Account WHERE Name IN :LeadNames]){
            
           mapAccountsName.put(A1.Name, A1);
        }
        
       Map<String,Account> mapAccountsPhone = new Map<String,Account>();
        for (Account A2 : [SELECT Id, Phone FROM Account WHERE Phone IN :LeadPhones]){
            
            mapAccountsPhone.put(A2.Phone, A2);
        }
        
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
        
        List<Database.LeadConvert> lstLC = new List<Database.LeadConvert>();
	  
        for (lead MyLead : myleads) {
            
 	        Account matchingAccountEmail = mapAccountsEmail.get(MyLead.Email);
            Account matchingAccountName = mapAccountsName.get(MyLead.Name);
            Account matchingAccountPhone = mapAccountsPhone.get(MyLead.Phone);
	 
            if(matchingAccountEmail != NULL){
         		Database.LeadConvert lc = new database.LeadConvert();
 				lc.setLeadId(MyLead.Id);
                lc.setDoNotCreateOpportunity(TRUE);
         		lc.setAccountId(matchingAccountEmail.Id);
                lc.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc);
            } else if (matchingAccountName != NULL){
                Database.LeadConvert lc1 = new database.LeadConvert();
 				lc1.setLeadId(MyLead.Id);
                lc1.setDoNotCreateOpportunity(TRUE);
         		lc1.setAccountId(matchingAccountName.Id);
                lc1.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc1);
            }else if (matchingAccountPhone != NULL){
                Database.LeadConvert lc2 = new database.LeadConvert();
 				lc2.setLeadId(MyLead.Id);
                lc2.setDoNotCreateOpportunity(TRUE);
         		lc2.setAccountId(matchingAccountPhone.Id);
                lc2.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc2);
                
            }        
        }
            
        if(!lstLc.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(lstLc);
            System.debug(+'result'+lcr); 
            
        } 
            
    }}

 
I have a Trigger and Helper Class that it calls which merges new leads into existing accounts if the email, name or phone matches an existing account. 

I'm trying to create a test class for the helper class but it's only at 70% coverage. What can I change about this testclass to have it achieve 100% coverage? 

Handler Class
public class DuplicateMergeTriggerHandler {
    
    public static void MergeHandler(Set<Id> leadIds) {
	
        List<String> LeadListEmails = new List<String>();
        List<String> LeadNames = new List<String>();
        List<String> LeadPhones = new List<String>();
        
        List<Lead> myleads = [SELECT Id, Name, Email, Phone FROM Lead WHERE ID IN :leadIds];
        
        for (Lead L1 : myleads) {
           LeadListEmails.add(L1.Email);
        }
		
        for (Lead L2 : myleads){
            LeadNames.add(L2.Name);
        }
        
        for (Lead L3 : myleads){
            
            LeadPhones.add(L3.Phone);
        }

        Map<String,Account> mapAccountsEmail = new Map<String,Account>();
         
        for (Account A : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN :LeadListEmails]){    
            mapAccountsEmail.put(A.PersonEmail, A);   
        }
        
        Map<String,Account> mapAccountsName = new Map<String,Account>();
        
        for (Account A1 : [SELECT Id, Name FROM Account WHERE Name IN :LeadNames]){
            
           mapAccountsName.put(A1.Name, A1);
        }
        
       Map<String,Account> mapAccountsPhone = new Map<String,Account>();
        for (Account A2 : [SELECT Id, Phone FROM Account WHERE Phone IN :LeadPhones]){
            
            mapAccountsPhone.put(A2.Phone, A2);
        }
        
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
        
        List<Database.LeadConvert> lstLC = new List<Database.LeadConvert>();
	  
        for (lead MyLead : myleads) {
            
 	        Account matchingAccountEmail = mapAccountsEmail.get(MyLead.Email);
            Account matchingAccountName = mapAccountsName.get(MyLead.Name);
            Account matchingAccountPhone = mapAccountsPhone.get(MyLead.Phone);
	 
            if(matchingAccountEmail != NULL){
         		Database.LeadConvert lc = new database.LeadConvert();
 				lc.setLeadId(MyLead.Id);
                lc.setDoNotCreateOpportunity(TRUE);
         		lc.setAccountId(matchingAccountEmail.Id);
                lc.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc);
            } else if (matchingAccountName != NULL){
                Database.LeadConvert lc1 = new database.LeadConvert();
 				lc1.setLeadId(MyLead.Id);
                lc1.setDoNotCreateOpportunity(TRUE);
         		lc1.setAccountId(matchingAccountName.Id);
                lc1.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc1);
            } else if (matchingAccountPhone != NULL){
                
                Database.LeadConvert lc2 = new database.LeadConvert();
 				lc2.setLeadId(MyLead.Id);
                lc2.setDoNotCreateOpportunity(TRUE);
         		lc2.setAccountId(matchingAccountPhone.Id);
                lc2.setConvertedStatus(convertStatus.MasterLabel);
                lstLc.add(lc2);
                
            }        
        }
            
        if(!lstLc.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(lstLc);
            System.debug(+'result'+lcr); 
            
        } 
            
    }}
Trigger
trigger DuplicateMergeTrigger on Lead (after Insert) {
    
    Set<ID> leadIds= new Set<ID>();
    
	for(Lead myLead : Trigger.New) {
        if(myLead.IsConverted == FALSE && myLead.Status == 'New' && myLead.College_Corps__c == TRUE){
            leadIds.add(myLead.Id);
        }
    } 

    if(leadIds.size() > 0 ) {
        DuplicateMergeTriggerHandler.MergeHandler(leadIds);
    }
}

Test Class
@istest
public class DuplicateMergeTriggerHandlerTest{
    
    static testmethod void testleadmerging(){
        
        Lead objLead = new Lead();
        objLead.LastName = 'Kim';
        objLead.FirstName = 'Koi';
        objLead.College_Corps__c = TRUE;
        objLead.Status = 'New';
        insert objLead;

       	Lead Lead2 = new Lead();
        Lead2.LastName = 'Test';
        Lead2.Email = 'salesforce12@gmail.com';
        Lead2.College_Corps__c = TRUE;
        Lead2.Status = 'New';
        insert Lead2;
        
        Lead Lead3 = new Lead();
        Lead3.LastName = 'Kim1';
        Lead3.Phone = '9125603000';
        Lead3.College_Corps__c = TRUE;
        Lead3.Status = 'New';
        insert Lead3;
        
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM Leadstatus WHERE IsConverted = TRUE LIMIT 1];
       
        Account acc1 = new Account();
        acc1.LastName = 'Kim';
        acc1.FirstName = 'Koi';
        insert acc1;
        
        Account acc2 = new Account();
        acc2.LastName = 'Kimbo';
        acc2.PersonEmail = 'salesforce12@gmail.com';
        insert acc2;
        
        Account acc3 = new Account();
        acc3.LastName = 'Test Test';
        acc3.Phone = '9125603000';
        insert acc3;
        
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(objLead.Id);
        lc.setDoNotCreateOpportunity(TRUE);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        lc.setAccountId(acc1.Id);
        
        Database.LeadConvertResult lcr = Database.convertLead(lc, false);
        
        System.assert(lcr.isSuccess());
 
        Database.LeadConvert lc1 = new database.LeadConvert();
        lc1.setLeadId(Lead2.Id);
        lc1.setDoNotCreateOpportunity(TRUE);
        lc1.setConvertedStatus(convertStatus.MasterLabel);
        lc1.setAccountId(acc2.Id);
        
        Database.LeadConvertResult lcr2 = Database.convertLead(lc1, false);
        
        System.assert(lcr2.isSuccess());
        
        Database.LeadConvert lc2 = new database.LeadConvert();
        lc2.setLeadId(Lead3.Id);
        lc2.setDoNotCreateOpportunity(TRUE);
        lc2.setConvertedStatus(convertStatus.MasterLabel);
        lc2.setAccountId(acc3.Id);
        
        Database.LeadConvertResult lcr3 = Database.convertLead(lc2, false);
        
        System.assert(lcr3.isSuccess());
        

        
    }    
 

}

 
I'm trying to query duplicate accounts through Anonymous Window. I'm doing this through querying the Aggregate result object.
I'm trying to get the Account Name and then compare it. In the debug log, I'm trying to query those duplicate accounts that match the Account Name in the AggregateResult in acctList2 but in the Debug Log it says 0. How should I approach this? 
 
AggregateResult[] Aggies123 = [select Name, count(Id) from Account WHERE RecordType.Name = 'Partners' GROUP by Name HAVING COUNT(Id)>1];
String AccountNaming;
List<Account> accList2 = new List<Account>();
List<Account> accList3 = new List<Account>();
for (AggregateResult Agg1 : Aggies123){

AccountNaming = String.valueOf(Agg1.get('Name'));                                             
}

AccountNaming = '%' + AccountNaming + '%';
accList2 = [SELECT Id, Name FROM Account WHERE Name LIKE :AccountNaming];
System.debug(accList2.size());

for(Account A1 : accList2){
    
    A1.PotentialDuplicate__c = TRUE;
    accList3.add(A1);
    
    
}

 
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);
        
              
            }

            
        }
            
        }
     
        
    }
   
}
I have a class to convert leads and I'm trying to write a test class for this so I can deploy to production, how should I approach this? 

Global class AutoConvertLeads {
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds){

        List<String> lstNames = new List<String>();
        for (User u : [SELECT Id FROM User WHERE Name = 'XXX' LIMIT 1]){
            
            lstNames.add(u.Id);
        }
        
    String Id = lstNames[0];
        
    Database.LeadConvert[] converts = new Database.LeadConvert[0];
    LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1];
    for(Lead record: [SELECT Id FROM Lead WHERE IsConverted = false LIMIT 1000]) { 
    Database.LeadConvert convert = new Database.LeadConvert();
    convert.setLeadId(record.Id); 
    convert.setConvertedStatus(convertedStatus.MasterLabel);
    convert.setOwnerId(XXX);
    convert.setDoNotCreateOpportunity(TRUE);
    converts.add(convert);
}
    
        if(!converts.isEmpty()){
            
            Database.convertLead(converts);
            
        }

        
       
      Integer listsize = converts.Size();
       DateTime NOW = system.NOW();
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {''};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Leads have been Converted, ' + NOW);
        mail.setPlainTextBody('Dear Admins, ' + listsize + ' Lead(s) were just converted into Person Accounts');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

    }
  
}
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);
        
              
            }

            
        }
            
        }
     
        
    }
   
}
I have a class to convert leads and I'm trying to write a test class for this so I can deploy to production, how should I approach this? 

Global class AutoConvertLeads {
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds){

        List<String> lstNames = new List<String>();
        for (User u : [SELECT Id FROM User WHERE Name = 'XXX' LIMIT 1]){
            
            lstNames.add(u.Id);
        }
        
    String Id = lstNames[0];
        
    Database.LeadConvert[] converts = new Database.LeadConvert[0];
    LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1];
    for(Lead record: [SELECT Id FROM Lead WHERE IsConverted = false LIMIT 1000]) { 
    Database.LeadConvert convert = new Database.LeadConvert();
    convert.setLeadId(record.Id); 
    convert.setConvertedStatus(convertedStatus.MasterLabel);
    convert.setOwnerId(XXX);
    convert.setDoNotCreateOpportunity(TRUE);
    converts.add(convert);
}
    
        if(!converts.isEmpty()){
            
            Database.convertLead(converts);
            
        }

        
       
      Integer listsize = converts.Size();
       DateTime NOW = system.NOW();
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {''};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Leads have been Converted, ' + NOW);
        mail.setPlainTextBody('Dear Admins, ' + listsize + ' Lead(s) were just converted into Person Accounts');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

    }
  
}