I've added a custom object named Organisation_Stat which is a child of Account. An Account may have zero or more related Organisation_Stat objects.
I want to enumerate Accounts which have related Org Stat objects which meet a criteria. Accounts with no Org Stats or Org Stats that do not meet the criteria will be omitted.
I have this query:
SELECT Account.Id, Account.Name, (SELECT Id, Name, Clients_Connected__c FROM Organisation_Stats__r S WHERE S.Clients_Connected__c > 3) Stats FROM Account
and in an action method in a controller, I'm trying to traverse the accounts and each accounts related set of Org Stats using this code, but it's not working:
public PageReference InvokeAsmxServiceV2() {
List<Account> queryResults = [SELECT Account.Id, Account.Name, (SELECT Id, Name, Clients_Connected__c FROM Organisation_Stats__r S WHERE S.Clients_Connected__c > 3) Stats FROM Account];
for (Account o : queryResults) {
/* Code for account omitted */
for (Organisation_Stat__c s : o.Stats) {
/* code for dealing with child org stat object omitted */
}
}
return null;
}
I've run this query in Codingo Studio, and the three output columns are "Id", "Name", and "Organisation_Stat__r" where the latter is a collection.
I don't think I should be using List<Account>
since its stopping me getting to the Organisation_Stat__r collection.
When I System.debug(System.JSON.serializePretty(queryResults))
there is an Organisation_Stat__r property on each Account populated with the data I want to process.
What should I change in the above to complete the traversal?