+ Start a Discussion

Test class Failure Error

I am getting below error while running testing .. recently i have updated Custom settings with additinal values and started getting the below error. please help if any one have idea.

Error Message System.LimitException: Too many SOQL queries: 101
Stack Trace Class.leadTriggerHandler.assignOwner: line 86, column 1
Class.leadTriggerHandler.onBeforeInsert: line 36, column 1
Trigger.LeadsTrigger: line 4, column 1

here is test class.
private class LeadTriggerHandlerTest {
    public static Map<String,User> testUsersMap;

    static testMethod void leadOwnerAssignmentTest() {
        Lead testLead = new Lead();
	        // Test null or blank zip code - owner should be default lead owner KCI US User
	          testLead = createLead('ABThera','Acute','');
	          system.assertEquals(SystemIdUtility.KCIUSUserId, [select OwnerId from Lead where Id=:testLead.Id].OwnerId);
        	// loop through all combinations as configured in Lead Assignment custom setting and create leads with the sample values
        	for(Lead_Assignment_Settings__c las : Lead_Assignment_Settings__c.getAll().values()){
	        	testLead = createLead(String.ValueOf(las.Product__c),String.ValueOf(las.Facility_Type__c),'63000');
	        	if(Limits.getQueries() < Limits.getLimitQueries()-20){
	        		if(testUsersMap.get(String.ValueOf(las.JCA__c)) != null){	
	        						[select OwnerId from Lead where Id=:testLead.Id].OwnerId);
        	// update Lead and test Owner assignment.
        	testLead.Products__c = 'Ulta';
        	testLead.Facility_Type__c = 'Acute';
        	update testLead;
        	// Test multiple product
        	testLead = createLead('Specialty Dressing;GRAFTJACKET','Acute','63000');
        	// Test non matching zipcode - then Lead Owner assigned should be KCI US User
        	testLead = createLead('ABThera','Acute','99999');
        	system.assertEquals(SystemIdUtility.KCIUSUserId, [select OwnerId from Lead where Id=:testLead.Id].OwnerId);
    private static Lead createLead(String product, String facilityType, String zipCode){
    	Lead newLead = new Lead();
    	newLead.FirstName = 'FirstName';
    	newLead.LastName = 'LastName';
    	newLead.Email = 'test@kci1.com';
    	newLead.Products__c = product;
    	newLead.Facility_Type__c = facilityType;
    	newLead.Zip_Postal_Code__c = zipCode;
    	insert newLead;
    	return newLead;
    private static void setupTestData(){
		//get test users
		testUsersMap = new Map<String,User>();
		list<User> testTMVUsers=[Select Id,JCA__C, Employee_Id__c from User where IsActive=true and Employee_Id__c!=null and JCA__C='TMV' and Id !=:Userinfo.getUserId() limit 1];
		if(testTMVUsers!= null && testTMVUsers.size()==1){
		list<User> testTSVUsers=[Select Id,JCA__C, Employee_Id__c from User where IsActive=true and Employee_Id__c!=null and JCA__C='TSV' and Id !=:Userinfo.getUserId() limit 1];
		if(testTSVUsers!= null && testTSVUsers.size()==1){
		list<User> testSSMUsers=[Select Id,JCA__C, Employee_Id__c from User where IsActive=true and Employee_Id__c!=null and JCA__C='SSM' and Id !=:Userinfo.getUserId() limit 1];
		if(testSSMUsers!= null && testSSMUsers.size()==1){
		//create Territory and Zip code for test user
		//create territory 
    	KCI_Territory__c testTerrTMV=new KCI_Territory__c(Name='terr-01', KCI_Legacy_Id__c='terr-001',Sales_Rep_Id__c=testUsersMap.get('TMV').employee_id__c, 
    	Territory_Code__c='TMV-test terr code',Territory_Start_Date__c=System.date.Today()-5, OwnerId=testUsersMap.get('TMV').Id);
    	KCI_Territory__c testTerrTSV=new KCI_Territory__c(Name='terr-02', KCI_Legacy_Id__c='terr-002',Sales_Rep_Id__c=testUsersMap.get('TSV').employee_id__c, 
    	Territory_Code__c='TSV-test terr code',Territory_Start_Date__c=System.date.Today()-5, OwnerId=testUsersMap.get('TSV').Id);
    	KCI_Territory__c testTerrSSM=new KCI_Territory__c(Name='terr-03', KCI_Legacy_Id__c='terr-003',Sales_Rep_Id__c=testUsersMap.get('SSM').employee_id__c, 
    	Territory_Code__c='SSM-test terr code',Territory_Start_Date__c=System.date.Today()-5, OwnerId=testUsersMap.get('SSM').Id);
    	insert new list<KCI_Territory__c>{testTerrTMV,testTerrTSV,testTerrSSM};
		//create zip codes
    	ZIP_Code__c testZipTMV=new ZIP_Code__c(Name='63000', KCI_Legacy_Id__c='zip-001',KCI_Territory__c=testTerrTMV.Id,Zip_Start_Date__c=System.date.Today()-5);
    	ZIP_Code__c testZipTSV=new ZIP_Code__c(Name='63000', KCI_Legacy_Id__c='zip-002',KCI_Territory__c=testTerrTSV.Id,Zip_Start_Date__c=System.date.Today()-5);
    	ZIP_Code__c testZipSSM=new ZIP_Code__c(Name='63000', KCI_Legacy_Id__c='zip-003',KCI_Territory__c=testTerrSSM.Id,Zip_Start_Date__c=System.date.Today()-5);
    	insert new list<ZIP_Code__c>{testZipTMV,testZipTSV,testZipSSM};

sherry pengsherry peng
Which line throws this error in test class? You can have up to 200 SOQL queries in a test method. 100 for test.starttest() .... Test.stoptest(). If the error throws out in before test.stoptest(), you can move some testing logic out. Another solution is that divide the test method to several test methods.  If these two solutions  cannot resolve your problem, you might need to review all related triggers and trigger handler classes to reduce SOQL.
its Happening in the line number 81. 

 insert new list<KCI_Territory__c>{testTerrTMV,testTerrTSV,testTerrSSM};
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior

Not sure but my best guess is that you perhaps have a SOQL statement inside a FOR loop in a trigger that involves the KCI_Territory__c object. You should always avoid SOQL inside loops even in tests but if you are preventing limits like you are doing I think it can be done though.

Furthermore, you now can take advantage of the annotation @testSetup to improve your test classes. Take a look at:


Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.