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
AsitAsit 

Help with the Test Class: System.LimitException: Too many SOQL queries: 101

Below is my test class.. though it is covering around 77% , still i am unable to resolve this error.

 

Message:-

System.LimitException: Too many SOQL queries: 101

 

 

Stack Trace:-

Trigger.UpdatePrimaryAccount: line 79, column 1

 

 

UpdatePrimaryAccount: Line 79:-

 Map<id,Account> personAccounts = new Map<id,Account>([Select Id, name, BillingStreet, BillingCity, BillingCountry, BillingPostalCode, PersonContactId, Person_Account_Brick_Name__c, Primary_Customer__pc, has_primary__pc from Account where PersonContactId in:locationContactId]);

 

My Test Class:-

 

@isTest
private class callReportCloneButtonTest{


 public static testMethod void callReportCloneButton(){ 

        Test.startTest();
               
        testUtils x = new testUtils();
        x.setupMasterData(); 
        List<Event> eventsToInsert1 = new List<Event>();
    eventsToInsert1.add(new Event(Subject = 'NewTestEvent', 
                        whoId = x.testCon.id, 
                        whatId = x.testAcc.id, 
                        DurationInMinutes = 30, 
                        ActivityDateTime = datetime.now()));
    insert eventsToInsert1;
    List<Call_Report__c> cr=[Select Id, duration__c from Call_Report__c where contact__c = :x.testCon.id];   
               
        Apexpages.Currentpage().getParameters().put('baId',x.testAcc.id);    
        Apexpages.Currentpage().getParameters().put('cont',x.testCon.id); 
        Apexpages.Currentpage().getParameters().put('crId',cr.get(0).Id);   
        Apexpages.Currentpage().getParameters().put('crRT','Call'); 
        Apexpages.Currentpage().getParameters().put('dt','16/11/2012 06:30'); 

          ApexPages.StandardController sc = new ApexPages.standardController(cr.get(0));
          callReportCloneButton crcb = new callReportCloneButton (sc);
          crcb.ct = x.testCon;
          crcb.getContactDetails();
          System.debug('hcpRecList ==> '+crcb.hcpRecList);
          for(callReportCloneButton.hcpRecordClass hrc:crcb.hcpRecList){
              hrc.blnHCPSelected=true;   
          }
          crcb.sel1=true;
          crcb.lname1='Test LName';
          crcb.phone1='90909099';
          crcb.job1='Dentist';
          
          crcb.sel2=true;
          crcb.lname2='Test LName1';
          crcb.phone2='90909099';
          crcb.job2='Dentist';
          
          crcb.sel3=true;
          crcb.lname3='Test LName2';
          crcb.phone3='90909099';
          crcb.job3='Dentist';
          
          crcb.insertHCP();
          Test.stopTest();
          
            
}
}

 

 

 

 

harsha__charsha__c

Hi Asit,

Error line number is 79, but it seems that it is causing due to the other part of your code

 

Check whethe you running any SOQL query within a loop..!

 

If not paste your class code here if possible..! It will be easier to find the cause

 

 

 

- Harsha

AsitAsit

Hi Harsha

 

I am not using any such queries inside loops..

Please suggest if I need to do any more amends!

 

Thanks!

APathakAPathak

 

Some points -

 

1. Put System.debug('###' + Limits.getAggregateQueries()); in test class to check where exactly are all your Queries being consumed. There might be recursion.

 

2. Put each test case in a separate method to reduce the Query consumption.

 

3. Put the actual test code inside Test.startTest() and Test.EndTest(). The test data creation part should be written outside these.

AsitAsit

The below code covers 89% but I am still not satisfied as I have to call my method 'insertHCP()' [highlighted in Red]outside Test.stopTest();

Suggest what can I do now? overall code coverage got decreased too.

 

 

 

 

@isTest
private class callReportCloneButtonTest{


public static testMethod void callReportCloneButton(){


Test.startTest();

testUtils x = new testUtils();
x.setupMasterData();
List<Event> eventsToInsert1 = new List<Event>();
eventsToInsert1.add(new Event(Subject = 'NewTestEvent',
whoId = x.testCon.id,
whatId = x.testAcc.id,
DurationInMinutes = 30,
ActivityDateTime = datetime.now()));
insert eventsToInsert1;
List<Call_Report__c> cr=[Select Id, duration__c from Call_Report__c where contact__c = :x.testCon.id];

Apexpages.Currentpage().getParameters().put('baId',x.testAcc.id);
Apexpages.Currentpage().getParameters().put('cont',x.testCon.id);
Apexpages.Currentpage().getParameters().put('crId',cr.get(0).Id);
Apexpages.Currentpage().getParameters().put('crRT','Call');
Apexpages.Currentpage().getParameters().put('dt','16/11/2012 06:30');

ApexPages.StandardController sc = new ApexPages.standardController(cr.get(0));
callReportCloneButton crcb = new callReportCloneButton (sc);
crcb.ct = x.testCon;
crcb.getContactDetails();
System.debug('hcpRecList ==> '+crcb.hcpRecList);
for(callReportCloneButton.hcpRecordClass hrc:crcb.hcpRecList){
hrc.blnHCPSelected=true;
}
crcb.sel1=true;
crcb.lname1='Test LName';
crcb.phone1='90909099';
crcb.job1='Dentist';

crcb.sel2=true;
crcb.lname2='Test LName1';
crcb.phone2='90909099';
crcb.job2='Dentist';

crcb.sel3=true;
crcb.lname3='Test LName2';
crcb.phone3='90909099';
crcb.job3='Dentist';


Test.stopTest();



try {
crcb.insertHCP();
} catch ( System.DmlException e) {
system.assert(e.getMessage().contains('System.LimitException: Too many SOQL queries: 101, System.LimitException: Too many SOQL queries: 101'),e.getMessage());
}


}
}

Starz26Starz26

Line 79 has nothing to do with the error. That is just the query that threw it over 100.

 

You will have to trace back all your code as well as existing code most likely from the point you perform any dml in your test method. Look for any code that is inside for loops. Look for any code that updates the same object (Including those where roll-up summaries update the record in after insert / update triggers), thus causing recursion.

 

Somewhere you have inefficient code executing.

AsitAsit

Hi Harsha

 

Well, calling crcb.insertHCP();  before the Test.stopTest(); is reducing the code coverage to 54% .


Thanks

 

Asit