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
CloudMikeCloudMike 

Using Database.countQuery() throws Warning when >= 500

I'm using the Database.countQuery() passing in a dynamic SOQL statement from a Visualforce page and output the results. For example:

 

String sSOQL = 'SELECT count() FROM Product2 LIMIT 500';
Integer iRecCount = 0;
iRecCount = Database.countQuery(sSOQL);

ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, sSOQL));

ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Record Count = ' + iRecCount));

 

Only when the resulting iRecCount is >= 500 is an apex:pageMessage warning message thrown:

"use countQuery() for [select count()...] queries" 

 

What's unexpected is I am using the countQuery()!

 

Anyone run into this before?  Any solution around it?

 

Thanks in advance,

Mike

 

 

 

imuinoimuino

You are counting the queries two times one inside the query and another when call to countQuery

 

Try this:

 

String sSOQL = 'SELECT Id FROM Product2 LIMIT 500';
Integer iRecCount = 0;
iRecCount = Database.countQuery(sSOQL);