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
Mitch McConnellMitch McConnell 

Too many SOQL queries and Apex tests

I know there are many answers to the "too many" part of this question, and I understand the reason and the normally suggested solutions.  
But I believe I see this behavior change even running the same test on different occasions, i.e., with no code changes sometimes it will get this error, and sometimes not.

Does this seem possible?  Has anyone else experienced this?

And yes, I have (many times) tried most of the suggested solutions....  so this is more of a "is this really possible?" question.​
 

Best Answer chosen by Mitch McConnell
sandeep@Salesforcesandeep@Salesforce
Mitch, 

It is same method giving differnet behavior then it might be possible there were changes made in your salesforce org components which are a part of execution flow of that test class. for example Trigger, class or workflow etc.
Please let me know what condition were when you run same test method two times.

​Thanks
Sandeep Singhal

All Answers

sandeep@Salesforcesandeep@Salesforce
Hi Mitch, 

When you run a particular test method it depends on it's nature that what part of your class it is covering up and accordingly your code flow gets executed. so it might be possible to run different - 2 execution for different test methods so some may cause too many SOQL some may not. 
If you need further clarification please let me know.

Thanks
Sandeep Singhal
http://www.codespokes.com/
Mitch McConnellMitch McConnell

Sandeep,

I am talking about the same method seemingly having different behavior, with no code changes.  I know it
doesn't make sense, but it sure seems like I have experienced this...

sandeep@Salesforcesandeep@Salesforce
Mitch, 

It is same method giving differnet behavior then it might be possible there were changes made in your salesforce org components which are a part of execution flow of that test class. for example Trigger, class or workflow etc.
Please let me know what condition were when you run same test method two times.

​Thanks
Sandeep Singhal
This was selected as the best answer
Vishal_GuptaVishal_Gupta
Hi Mitch,

Is your test class is creating its own test data or are you fetching data from system. If seeAllData true and you are using system data then there is a chance that too many SOQL depend on the data coming from system in differrent execution and so sometine issue will come and sometime not.

I would suggest you to keep seeAllData= false in your test class.

Thanks,
Vishal
Mitch McConnellMitch McConnell

Sandeep, thanks for taking the time to reply.  I know this does not make sense.  This is in a sandbox that I am the sole
developer on...  I am guessing that I will simply have to review each trigger and the code being executed to find this, but this 
is extremely complex... far too much to try and post anything... Thanks.. Mitch

sandeep@Salesforcesandeep@Salesforce
Yes because some time what happen either we use SOQL in loop or triggers calls it self recursively. But Any way if you feel this error again please provide me trigger code or access of your org ( if you can ) so that I can explore more.

and Vishal's suggestion would help you also.

Thanks 
Sandeep Singhal
sandeep.salesforce
http://www.codespokes.com/