I am trying to do query that displays information from a children object only when the parent has more than one children, meaning the child record has siblings (like filtering by the Parent’s Roll-up field), but the relationship is a look-up field rather than master-detail.
In theory, this query should do the work, but this is not a runnable query:
SELECT FIELDS(ALL) FROM childrenObject WHERE id IN (SELECT parentId, COUNT(id) FROM childrenObject GROUP BY parentId HAVING COUNT(id)>1)
- I tried removing the "SELECT COUNT(id)" from the condition subquery, so the only information gathered is "SELECT parentId", but this doesn't work either.
- Only running the subquery will give me the ParentId and the count, I need other fields from the child object.
I also tried a query from the Parent Object, nesting the children's but I am not able to see how to filter only the parents with more than one record, they are totalised in a column called "childRelationship.totalSize" in the query result, and I am not able to use this column as a filter condition. eg.
SELECT id, (SELECT FIELDS(ALL) FROM childRelationship) FROM parentObject WHERE childRelationship.totalSize > 1 ;
**I am aware fields(all) will need a limit, I just using fields(all) like a example that I need more than just one field returned in the query.
Forum Ambassador Eric Burté (DEVOTEAM)
@Jose Carlos De Oliveira, It seems as a SOQL limitation. I suppose you will need to run a second SOQL / or to directly run APEX behind (according to your architecture) to loop on the retrieved parentObject/childrenObjects, and then filter the parent object collection based on the number of children.
Eric