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

Too many rows error when executing database.CountQuery()

Can *anyone* tell me why a count query would generate a "System.Exception: Too many query rows: 10001" message?


It breaks here: integer intCount = Database.countQuery(strQuery); //execute the SOQL Count Query


Here's the annotated code:



public List<Transport__c> getDisplay() { Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe(); //Get Describe object for 'ACCOUNT' List<Schema.ChildRelationship> C = R.getChildRelationships(); //Get List of child relationships for 'ACCOUNT' List<Transport__c> transports = new List<Transport__c>(); //initialize list of placeholder objects For (Schema.ChildRelationship child : C) //For each Child relationship -- 52 total { Schema.DescribeSObjectResult R2 = (child.getChildSObject()).getDescribe(); //Get Describe object for the related child object boolean boolQuery = R2.isQueryable(); //Is the Child object Queryable? if (boolQuery) //If so -- 48 total { Transport__c transport = new Transport__c(); //initialize placeholder for this object transport.Text_1_80__c = string.valueOf(child.getChildSObject()); //Get the child's object token and convert it to string transport.Text_2_80__c = string.valueOf(child.getField()); //Get the child's foreign key field token and convert it to string string strObject = string.valueOf(child.getChildSObject()); //ditto string strField = string.valueOf(child.getField()); //ditto string strValue = '0015000000QKSb1AAH'; //hard coded Account ID for testing string strQuery = 'SELECT Count() FROM ' + strObject + ' WHERE ' + strField + ' = \'' + strValue + '\''; //Create SOQL count query string integer intCount = Database.countQuery(strQuery); //execute the SOQL Count Query transport.Title_1_255__c = strQuery; //Display the SOQL query string transport.Text_3_80__c = string.valueOf(intCount); //Display the string value of the count result transports.add(transport); //add this placeholder to placeholder list } } return transports; //send the list to the page for display }


 This makes ZERO sense. Code works fine if I comment out the query execution.



I don't think this has been fixed yet but I could be wrong. SOQL actually returns the rows and just calls size(). Thats why you get a query row exceeded exception.


Here is also the idea posted about it.


Thanks! I actually figured this out and got around it by adding LIMIT 1 to my query. All I cared about was whether the Id was found, not how many times it was found. This should definitely be changedQ We should not be "charged" for data we're not actually getting. :-)