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
HaroldHHaroldH 

SELECT COUNT(*) functionality using batch size = 1 - recommended?

I can get the count of records in an sforce object by following these steps:

1. Set QueryOptions batch size to 1
2. SOQL SELECT without a WHERE clause, to return all records
3. Check queryResult.getSize() (.size property) to find the record count
(4. Reset QueryOptions to a more reasonable size, e.g., 500 or 2000.)

Does Salesforce have an opinion on this approach, i.e., is it not recommended due to the side effect of creating a QueryLocator that will go unused?

Or is there another preferred method for getting the number of records in an sforce object?
DevAngelDevAngel

Hi HaroldH,

Well, you have to do something until such time as aggregate functions are included in SOQL.  I think you are using the most reasonable workaround to missing aggregates.  The cursor cost is something we have to pay until we give you what you need. 

HaroldHHaroldH
Thanks for that, Dave.

Here's a related question on the cursor/QueryLocator, then.

Say we select a large result set, and begin processing it using queryMore(). Before we get the last batch in the result set, the session times out. Do we lose the cursor at that time? Or can we relogin and continue where we left off?
SuperfellSuperfell
No you don't loose the cursor, its tied to your userId, not sessionId.