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
Neeraj Sharma 103Neeraj Sharma 103 

Hi Everyone how to write test class for below trigger with Trigger handler

Trigger

trigger LeadTrigger on Lead(before insert, before Update) {

    if (trigger.isInsert && trigger.isBefore) {
        LeadTriggerHandler.onBeforeInsert(trigger.new);

    } else if (trigger.isUpdate && trigger.isBefore) {
        LeadTriggerHandler.onBeforeUpdate(trigger.new, trigger.oldMap);
    }
}
 
TrigerHandlerClass

public class LeadTriggerHandler {
  
    public static String STATUS_SELF_REPRESENTED = 'Self Represented';
    
  
    public static void onBeforeInsert(list <Lead> newLeadList){
        updateFldsOnLead(newLeadList, null);
    }

   
    public static void onBeforeUpdate(list <Lead> newLeadList, map <Id, Lead> oldLeadMap){
        updateFldsOnLead(newLeadList, oldLeadMap);
    }

    public static void updateFldsOnLead(list <Lead> newLeadList, map <Id, Lead> oldLeadMap){
        map<String, list<lead>> zipCodeLeadMap = new Map <String, list<lead>> ();

       for(Lead ld: newLeadList){
            if(ld.Status != STATUS_SELF_REPRESENTED && ld.PostalCode != null){
              
               
                if(!zipCodeLeadMap.containsKey(ld.PostalCode)){
                    zipCodeLeadMap.put(ld.PostalCode, new list<lead>());
                }
                zipCodeLeadMap.get(ld.PostalCode).add(ld);
            }

        }
        if(zipCodeLeadMap.size() > 0){
           
            for(Lead_WF_Configuration__mdt ldConfigCMD: [Select Id, Zip_Postal__c, State__c, County__c,OwnerID__c
                                                          From Lead_WF_Configuration__mdt]){
                                                
                for(String zip: zipCodeLeadMap.keySet()){
                    
                    if(String.valueOf(ldConfigCMD.Zip_Postal__c).contains(zip)){
                        
                        
                        for(Lead ld: zipCodeLeadMap.get(zip)){
                            ld.State = ldConfigCMD.State__c;
                            ld.County__c = ldConfigCMD.County__c;
                            ld.OwnerId = ldConfigCMD.OwnerID__c;
                        }
                 }
                 }
            }
        }
    }
}

 
Best Answer chosen by Neeraj Sharma 103
Sunil RathoreSunil Rathore
Hi Neeraj,

Use the following test class. Please update the data and add assert as per your requirement:
@isTest
public class TestLeadTrigger{
    
    public testmethod  static void testLeadInsert (){
        Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
		
		Test.startTest();
	    objLead = new Lead (LastName = 'Test data', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
		Database.SaveResult result = Database.insert(objLead, false);
		//add System.assert() with your condition
		Test.stopTest();
    }
	
    public testmethod  static void testLeadUpdate (){
        Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
	    Lead objSecondLead = new Lead (LastName = 'Test data', Email = 'testother@gmail.com', mobilephone = '+1234567800', Company = 'Test company');
        insert objSecondLead; 

		Test.startTest();
		objSecondLead.Email = objLead.Email;
		objSecondLead.mobilephone = objLead.mobilephone;
		Database.SaveResult result = Database.update(objSecondLead, false);
		//add System.assert() with your condition
		Test.stopTest();
    }
}

Let me know if it solves your problem.

Many Thanks,
Sunil Rathore

All Answers

Sunil RathoreSunil Rathore
Hi Neeraj,

Use the following test class. Please update the data and add assert as per your requirement:
@isTest
public class TestLeadTrigger{
    
    public testmethod  static void testLeadInsert (){
        Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
		
		Test.startTest();
	    objLead = new Lead (LastName = 'Test data', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
		Database.SaveResult result = Database.insert(objLead, false);
		//add System.assert() with your condition
		Test.stopTest();
    }
	
    public testmethod  static void testLeadUpdate (){
        Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
	    Lead objSecondLead = new Lead (LastName = 'Test data', Email = 'testother@gmail.com', mobilephone = '+1234567800', Company = 'Test company');
        insert objSecondLead; 

		Test.startTest();
		objSecondLead.Email = objLead.Email;
		objSecondLead.mobilephone = objLead.mobilephone;
		Database.SaveResult result = Database.update(objSecondLead, false);
		//add System.assert() with your condition
		Test.stopTest();
    }
}

Let me know if it solves your problem.

Many Thanks,
Sunil Rathore
This was selected as the best answer
Neeraj Sharma 103Neeraj Sharma 103
Hi Sunil Rathore
Your Code Coverage is 47% How to cover below part
if(!zipCodeLeadMap.containsKey(ld.PostalCode)){
                    zipCodeLeadMap.put(ld.PostalCode, new list<lead>());
                }
                zipCodeLeadMap.get(ld.PostalCode).add(ld);
            }
        }
        if(zipCodeLeadMap.size() > 0){
            //Fetching Meta Data from Custom Meta Data Type and iterate in loop
            for(Lead_WF_Configuration__mdt ldConfigCMD: [Select Id, Zip_Postal__c, State__c, County__c,OwnerID__c
                                                          From Lead_WF_Configuration__mdt]){
                //Building a Key Set                                          
                for(String zip: zipCodeLeadMap.keySet()){
                    
                    if(String.valueOf(ldConfigCMD.Zip_Postal__c).contains(zip)){
                        
                        //Get Lead Records of Same ZipCode and then Upate its Fields
                        for(Lead ld: zipCodeLeadMap.get(zip)){
                            ld.State = ldConfigCMD.State__c;
                            ld.County__c = ldConfigCMD.County__c;
                            ld.OwnerId = ldConfigCMD.OwnerID__c;
                        }
                	  }//end i
Sunil RathoreSunil Rathore
Hi Neeraj,

In testLeadUpdate method ;
add values for Status and Postal Code in the lead record i.e in objLead and objSecondLead.

Also, take care that Status value should satisfy this condition ld.Status != STATUS_SELF_REPRESENTED.

Let me know if this works for you.

Many Thanks,
Sunil Rathore
Neeraj Sharma 103Neeraj Sharma 103
Hi Sunil Rathore
Can you tell me how to write and which condtion i  write in system.assert() method

thanks
Neeraj Sharma
Sunil RathoreSunil Rathore
Hi Neeraj,

Use the following lines in both methods:
Lead objLeadQueried = [Select ID, State from lead where lastName = 'Test data'];
System.assertEquals(objLead.State , Your value of state in meta data);
 Let me know if it runs successfully.

Many Thanks,
Sunil Rathore





 
Neeraj Sharma 103Neeraj Sharma 103
Hi Sunil Rathore

Thanku So Much Now its Working 

Thanks
Neeraj Sharma