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
XXXXXX 

How Do I Get A Record Count?

Using the sample code from the Apex Language reference, I am trying to find out how many Contact records I have:

 

Integer contactCount = [SELECT COUNT() FROM Contact];

 

This raises an error "Too many query rows: 501" -- which implies that I cannot get a simple rowcount on tables with more than 500 rows. Is this true? Do I actually have to use a loop and queryMore() to find out how many rows I have in a table?

 

TIA,

 

John

Best Answer chosen by Admin (Salesforce Developers) 
hisrinuhisrinu
I think you have tried in test method, am I right? Test methods can't retrieve more than 500 query rows

All Answers

zeezackisbackzeezackisback
I have a similar problem, how does one go about trying to solve this?
Message Edited by zeezackisback on 05-05-2009 08:42 AM
hisrinuhisrinu
It will retrieve till 10000, if you have more than 10000 records then it will throw error.
hisrinuhisrinu
I think you have tried in test method, am I right? Test methods can't retrieve more than 500 query rows
This was selected as the best answer
XXXXXX
I can work around the test method constraint, now that I know about it -- but what do I do if I have more than 10000 rows in a table and I need to know exactly how many rows I have?
thecrmninjathecrmninja

Maybe this call combined with an integer variable?

queryMore()

Retrieves the next batch of objects from a query().

Syntax

QueryResult = binding.queryMore( QueryLocator   QueryLocator);

Usage

Use this call to process query() calls that retrieve a large number of records (by default, more than 500) in the result set. The query() call retrieves the first 500 records and creates a server-side cursor that is represented in the queryLocator object. The queryMore() call processes subsequent records in up to 500-record chunks, resets the server-side cursor, and returns a newly generated QueryLocator. To iterate through records in the result set, you generally call queryMore() repeatedly until all records in the result set have been processed (the Done flag is true). You can change the maximum number of records returned to up to 2,000. See Changing the Batch Size in Queries for more information.

 

 

XXXXXX
I certainly hope I don't have to retrieve all of the rows in my table just to find out how many rows I have. That's like withdrawing all of the money in your checking account just to balance your checkbook.
thecrmninjathecrmninja
I've never used this call, but I would imagine your not talking about too much more code.  You just iterate and update an Integer variable and count it at the end. 
GregWaxGregWax

Isn't the querymore() for the API calls? 

 

How do you get a count() using SOQL in APEX? 

 

I have a large database and I get errors when I reach 50001 rows using For SOQL loops.

KevinBr82KevinBr82

 

I came across this need as well, and have constructed a solution:

Depending on your needs, you may prefer the dynamic dml solution vs the traditional (soql) solution.

 

//Method 1 

string soql='select count(id) from Case';

AggregateResult res = Database.Query(soql);

Integer count = integer.valueof(res.get('expr0'));

 

//Method 2

AggregateResult res = [select count(id) from Case];

Integer count = integer.valueof(res.get('expr0'));

 

Props:

http://boards.developerforce.com/t5/forums/replypage/board-id/apex/message-id/15550

 

 

Cheers,

-Kevin  

 

 

 

NeerpalNeerpal

when i hit on a go button for All Accounts view, is there a way i can get the total record count of the accounts somewhere on the page like Previous Next