I need to do a group by cube on a related list. What I want is to display a list of added services groups and the distinct count across all completed inspections by package, and the matrix should have the groups along the y axis with the packages along the x axis. I'm getting an error Expecting '}' but was : 'for'

public class AddedServices3 {

public list<Inspection__c> inspections = [SELECT Id, (SELECT Added_Services_Group__c, Inspection_Package__c, Id
                                   FROM Inspection_Services__r)
                                   FROM Inspection__c
                                   WHERE Status__c = 'Completed' AND Month_Since_Launch__c > 0];   

for(Inspection__c i: [SELECT id(SELECT Id, Added_Services_Group__c, Inspection_Package__c FROM Inspection_Services__r) FROM Inspection__c]){
    for(Inspection_Service__c addedservices : i.Inspection_Services__r){
        public list<AggregateResult> listAr = new list<AggregateResult>(
                [SELECT Added_Services_Group__c AddedService, count_distinct(id) cnt, Inspection_Package__c Package,
                GROUPING(Added_Services_Group__c), GROUPING(Inspection_Package__c) 
                FROM Inspection_Service__c
                GROUP BY CUBE (Added_Services_Group__c, Inspection_Package__c)]
  • SOQL query within for loop is bad Mar 20, 2018 at 0:52
  • May or may not be related (sometimes the compiler gets confused and gives the "wrong" error message), but Inspection_Services__r is not a type name (Inspection_Services__r services).
    – David Reed
    Mar 20, 2018 at 0:52
  • Actually nothing works after 'for.' If I remove everything afterward, as soon as I type 'for' I get the error.
    – Sebastian
    Mar 20, 2018 at 1:17

1 Answer 1


The error you're getting is apparently because you confused the parser. Simply, you missed a comma:

for(Inspection__c i: [SELECT id,(SELECT Id, Added_Services_Group__c, Inspection_Package__c FROM Inspection_Services__r) FROM Inspection__c]){
                             // missing comma...

You must log in to answer this question.

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