I am running into a very strange error where I am getting an exception in an Apex controller when I am trying to access the results of a relationship query.

I have narrowed down this issue to specifically be the cause of trying to access the results of a query on a concrete knowledge article version. In this query, I am retrieving a custom field of type File, as well as its data category selections through a relationship query. The exception is thrown when I use the native getSObjects('fieldName') function on the result of the query to get the result of the relationship query. The exception thrown is this -

System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop.

I have run the query (shown in the example at the bottom) in the Query Editor in the Developer Console and can see that the results of the relationship query return what I expect - a list of DataCategorySelection objects.

Curiously enough, I noticed that this exception is only thrown when the knowledge articlce has more than one data category selected, and I am trying to query the subfields of a custom field of type File in conjunction when its data category selections. If I try to use getSObjects() on the result of a query where there is only one data category is selected, or multiple are selected but I do not query any File subfields, then this exception is not thrown.

Steps to reproduce

  • Create an article type
  • For that article type, create a custom field of type File
  • Create at least two data category groups, each with one data category
  • Create an article that has a data category assignment from each of the two data category groups (so 2 data categories in total), and that has a file uploaded to that file custom field
  • Publish the article
  • In anonymous apex, query the article with a relationship query retrieving its data category selections
  • In the subsequent line, call getSObjects() to retrieve the results of the relationship query
  • An exception will be thrown - "System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop.

An example of some Apex that describes the last two bullet points -

List<SObject> articleList = [
    SELECT Supporting_Document__Name__s, 
        (SELECT ParentId, DataCategoryGroupName, DataCategoryName 
        FROM DataCategorySelections), Supporting_Document__ContentType__s, 
        Supporting_Document__Length__s, Supporting_Document__Body__s 
    FROM General_Article__Kav
// Exception is thrown in the next line

1 Answer 1


First thing to try based on the error message is this:

for (DataCategorySelection s : articleList.get(0).getSObjects('DataCategorySelections')) {
    // Do something with each "s"

where groups of records will be fetched multiple times as part of the built-in SOQL For Loops mechanism.

I don't understand why "Aggregate query" is mentioned in the error message though,

  • Interesting... doing what you suggested above yields a different exception: "Line 41, Column 1 | System.QueryException: invalid query locator". I'm not too sure what to make of this though, apart from the fact the system can't chunk properly with query() or queryMore()
    – alecln
    Commented May 15, 2018 at 20:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .