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 System.debug(articleList.get(0).getSObjects('DataCategorySelections'));