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
Mike_MMike_M 

Too many query rows on COUNT(*) function

My main question is, how can I get the following counts:

  1) all accounts with no parent account

  2) all accounts with a parent account

  3) all accounts

 

Here is what I have tried:

Using the Developer Console in SalesForce gave the following commands ...

   integer c1 = [select count() FROM Account where parent.id = null ];

   integer c2 = [select count() FROM Account where parent.id <> null ];

   integer c3 = [select count() FROM Account ];

 

The first two commands were successful but on "integer c3 = [select count() FROM Account ];" I hit a limit.

11:02:17:378 EXCEPTION_THROWN [1]|System.LimitException: Too many query rows: 50001

 

So, how can I get the total count of all accounts? (using or not using Developer Console) 

 

TIA,

Mike

 



SeAlVaSeAlVa

Extract from Apex Code Developer's Guide 

 

Queries that include aggregate functions are subject to the same governor limits as other SOQL queries for the total number of records returned. This limit includes any records included in the aggregation, not just the number of rows returned by the query. If you encounter this limit, you should add a condition to the WHERE

 clause to reduce the amount of records processed by the query

  

I think you can try something like the following (if you don't have offset enabled for your organization, you will need to request it through a case). 

(I apologize if it needs some changes, but I did not have the chance to test it as I don't have offset enable)

 

Integer total = 0;
Integer current = 0;
do{
    current = [select count() from Account LIMIT 50000 OFFSET :total]
    total += current;
}while(current == 50000);

 (your c3 would be total variable)

 

Anyway, isn't c3 = c1 + c2 ?

 

Regards

Mike_MMike_M

Thanks SeAIVa,

I have submitted a ticket to thave offset turned on.

And, yes, you are correct, C3 should be C1 + C2. I just wanted C3 to PROVE that I didin't miss anything. 

 

It's hard to believe that it is so difficult to get something as simple as how many accounts I have. <sigh>

 

Anyway, thanks for your help. I'll wait for OFFSET to be enabled.

 

Regards,

Mike

 

PS, I'm going to wat a bit on accepting your reply as a solutuion. Maybe somebody else has a different approach. I've been trying to create a report that would group by parent_id as either a null or non-null value, but so far no success as I'm a total noob. 

 

 

 

SeAlVaSeAlVa

I'm afraid that won't work. 

I've just read something about offset that I didn't know.

 

The maximum offset is 2000 rows

 


Mike_MMike_M

Well, it this case, it would actually work as there are only aroung 5,000 rows. If I set offset at 2000 and then get count()= 3210, I will know there are 5210 rows. :-)

 

 

BritishBoyinDCBritishBoyinDC

If you use a VF Page and Controller, and set the VF page to readonly on the page, you can query up to 1 000,000 rows...