I would like to use aggregate query in batch job but I'm running into an error:
HAVING expression must be grouped or aggregated
Would also like to understand how can I use count from the query when I'm looping through the record. Here is code for my batch job:
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([SELECT Account__c, COUNT(Name) FROM MyCustomObject__c GROUP BY Account__c HAVING COUNT(Name) > 0 AND CustomField__c = 'myValue' ORDER BY Account__c]);
}
global void execute(Database.BatchableContext bc, List<SObject> batch) {
Map<Id, Integer> accCoops = new Map<Id, Integer>();
for (MyCustomObject__c h : (List<MyCustomObject__c>) batch) {
accCoops.put(h.Account__c, h.COUNT(Name));
}
}
When I update query locator query to:
[SELECT Account__c, COUNT(Name) FROM MyCustomObject__c GROUP BY Account__c HAVING COUNT(Name) > 0 ORDER BY Account__c]
I run into following error:
Argument cannot be an aggregate result inline query
Please advice why I'm not able to use HAVING with AND as shown here: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_having_considerations.htm
Is there any workaround for this?