+ Start a Discussion
Aishwarya MashaleAishwarya Mashale 

I am getting too many SOQL error in the test class method

@isTest private static void test_send() {
        
        Account countryTeam = AIM_TestDataUtility.accCountryTeamRecord('AIM_Notification_Mailer_Test');
        insert countryTeam;
        Test.startTest();
        List<AccountTeamMember> countryTeamMembers = AIM_TestDataUtility.accCountryTeamMembers(countryTeam.Id);
        insert countryTeamMembers;
        EmailTemplate template = [SELECT Id, DeveloperName FROM EmailTemplate WHERE IsActive = true LIMIT 1];
        Test.stopTest();
VinayVinay (Salesforce Developers) 
HI Aishwarya,

By using the Test.startTest() & Test.stopTest() you get 2X governer limits.

Below are check points.

> Optimize and see if you can reduce the queries.
> Reduce the number of objects you are inserting and you can skip unnecessary trigger execution by checking if trigger being called by Test method if(Test.isRunningTest())
> In worst case you can use SeeAllData = ture(not best practice)

Check below reference for more details.

https://help.salesforce.com/s/articleView?id=000331875&type=1

Please mark as Best Answer if above information was helpful.

Thanks,
mukesh guptamukesh gupta
Hi Ashwarya,

I think you are using SOQL in loop, so you need check class AIM_TestDataUtility and method accCountryTeamMembers

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 




 
Shubham Jain 338Shubham Jain 338
Hi Aishwarya,

Use Limits.getQueries() method and debug in class and in test class code and check at which point your code is performing too much SOQL and accordingly optimize the code so that it won't execute more than 100 SOQL in a transaction. 

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

Check if you are performing SOQL in any loop then perform SOQL outside the loop.

If you have implemented the @TestSetup method then check how much query it is performing and accordingly shift the Test.startTest() and Test.stopTest().

Kindly mark my solution as the best answer if it helps you.

Thanks
Shubham Jain