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
kpetersonkpeterson 

SOQL Count() query causes "Too many query rows: 501" error?

I've deployed all my triggers and classes to a full sandbox (structure and data) without any problems.  All my tests create their own test objects/data so it doesn't rely on instance data.  This one in particular uses two hardcoded users that exists in all instances but since I'm creating the test accounts any existing data should not interfere with the queries.

Yet when I try to deploy to our production instance I get a "too many query rows: 501" error on this line:


Code:
System.assertEquals(60, [SELECT COUNT() FROM SFDC_Assignment__c WHERE Account__c IN :oAccounts AND OwnerId = :oOriginalUser.Id AND Resource__c = :oOriginalUserResource.Id]);

 Is this error saying that too many rows are being returned?  The documentation says that 1,000 is the max number of records that can be retrieved through a query but it's flagging it on 501 and this is a COUNT() query so it shouldn't matter.


Message Edited by kpeterson on 12-12-2007 06:30 AM
Best Answer chosen by Admin (Salesforce Developers) 
kpetersonkpeterson
I ended up modifying my tests to play with less data.  I still don't know why it was going over on a count, it might have been something occuring before it.  I did realize that when deploying to a different sandbox no tests are run (you have to manually run them after they have been deployed there).  When you deploy to a production environment it automatically runs the tests which is why I got my error only when I deployed to prod and not sandbox.  Also, I realized that the limits are lower for test runs compared to live interactions so the limit really was 500.

All Answers

ssikorassikora
Check the "Execution Governors and Limits" section for a detailed description on why you may be getting this error.
kpetersonkpeterson
I have reviewed it but I haven't been able to find anything that applies.
kpetersonkpeterson
I can't find anything talking about 500 as a limit for anything.

This can't be a limit of count() because it can return much larger numbers.
mahimahi
Hi KPeterson,
 
i am facing same problem. Do you get some solution for this problem?
 
Please help me.
 
thanks,
mahi
kpetersonkpeterson
I ended up modifying my tests to play with less data.  I still don't know why it was going over on a count, it might have been something occuring before it.  I did realize that when deploying to a different sandbox no tests are run (you have to manually run them after they have been deployed there).  When you deploy to a production environment it automatically runs the tests which is why I got my error only when I deployed to prod and not sandbox.  Also, I realized that the limits are lower for test runs compared to live interactions so the limit really was 500.
This was selected as the best answer
Abhinav GuptaAbhinav Gupta

The key to get this error fixed is ISOLATION of your test data from the org's data. Try executing your test via System.runAs(<User>). I am sure it will help.

This blog post explains this in more detail : http://www.tgerm.com/2010/05/systemrunas-501-apex-query-rows.html