+ Start a Discussion
mworldmworld 

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.

 

AmphroAmphro

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.

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select.htm#i1422081

 

Here is also the idea posted about it.

http://ideas.salesforce.com/article/show/10089055/Count_the_SOQL_count_query_as_a_single_row_query

 

mworldmworld
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. :-)