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
Moran AssaMoran Assa 

System.Exception: Too many SOQL queries: 101 in test class!

We have a test class with (SeeAllData=true),
The class recently started failing due to 'Too many SOQL queries: 101 in test class'.

We understand the problem in the code but now we don't seem to be able to update the test class on the live organization 
(In a change set from the sandbox) because the test fails on deploy.

Any way to solve this issue?

Thanks

Best Answer chosen by Moran Assa
Sumitkumar_ShingaviSumitkumar_Shingavi
I think you are mis-understanding the issue. Your test coverage might be inserting/updating/deleting some data which might be kicking some triggers and if your triggers are not bulkified or have SOQL in forloops then this will happen. You might be obeserving it issue from test classes but it is actual code which is causing issue which is getting tested through that test class. Hope this helps!

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
Yes, this happens sometimes because you have some new code from other SBs which your didn't had in your SB. I am sure that you are directly trying to deploy code from DEV to Prod and so your didn't seen it before. If you had most receint UAT and you did followed DEV=>UAT=>Prod then you will definately see it.

Only solutions to this can be:
1. See where it is hitting so many SOQLs through debug logs and optimise code for same so re-deploy after refactoring
2. Follow best proctices for deployments for deployments and bulk testings. So, get a fresh UAT and work in it to see why it happening.
Moran AssaMoran Assa
Hey Sumitkumar,

Thanks for your reply.

We know where in the code the problem occurs. we can't do anything about that though, because when we try to deploy a fixed version of the test class, the deploy process fails (for too many SOQL queries).

Is there a way we can disable a test on the live organization, just so we can upload a fixed version of the test?
Sumitkumar_ShingaviSumitkumar_Shingavi
I believe, if you really know where is the issue in code and what is starting point; you can deactivate that. Like if it is a trigger then disable that trigger for deployment and then re-activate it again.
Moran AssaMoran Assa

The problem is in a test class (that runs automatically on deploy), is there a way to disable that class?

Thanks

Sumitkumar_ShingaviSumitkumar_Shingavi
I think you are mis-understanding the issue. Your test coverage might be inserting/updating/deleting some data which might be kicking some triggers and if your triggers are not bulkified or have SOQL in forloops then this will happen. You might be obeserving it issue from test classes but it is actual code which is causing issue which is getting tested through that test class. Hope this helps!
This was selected as the best answer
Parvinder SinghParvinder Singh
OOB way of handling this will be to put the code thats actually going the coverage in the System.Starttest(); and System.stopTest(); methods. https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm

If you still hit this error other hacky way to bypss triggers from test class run is use of static variables, in your test class define a static variable, set it to true and then in your triggers add a line at the top that will check this static flag and bypass them if the static variable is set to true.
Note - Only bypass the triggers you dont need and are not trying to cover with this method.
 
Moran AssaMoran Assa

Thanks for the help,

Got it working now