You need to sign in to do that
Don't have an account?
Laurie Drew
Error trying to build a map
Hi,
I am trying to build a map that will contain the Account OwnerId as the key and the sum value of the field Amount grouped by Month and Year as the value and receiving the error:
"Loop variable must be a generic SObject or LIst or a concrete SObject or List of: AggregateResult"
Here is my code, can anyone help me fix what I am doing wrong?
Map<Id,Decimal> invoices = new Map<Id,Decimal>();
for(Invoice__c obj:[Select Account__r.OwnerId, sum(Amount__c) FROM Invoice__c group by Account__r.OwnerId, Calendar_Year(InvoiceDate__c), Calendar_Month(InvoiceDate__c)]){
invoices.put(obj.Account__r.OwnerId,obj.Amount__c);
}
I am trying to build a map that will contain the Account OwnerId as the key and the sum value of the field Amount grouped by Month and Year as the value and receiving the error:
"Loop variable must be a generic SObject or LIst or a concrete SObject or List of: AggregateResult"
Here is my code, can anyone help me fix what I am doing wrong?
Map<Id,Decimal> invoices = new Map<Id,Decimal>();
for(Invoice__c obj:[Select Account__r.OwnerId, sum(Amount__c) FROM Invoice__c group by Account__r.OwnerId, Calendar_Year(InvoiceDate__c), Calendar_Month(InvoiceDate__c)]){
invoices.put(obj.Account__r.OwnerId,obj.Amount__c);
}
Since you have Aggregate function "Sum" in your query you have to use AggregateResult. Here is the code to fetch the values.
Regards
Karthik
All Answers
I would investigate how to use the AggregateResult feature of Salesforce/Apex.
Basically, you query is returning an AggregateResult list not a list of Invoice Objects.
Regards
Andrew
Since you have Aggregate function "Sum" in your query you have to use AggregateResult. Here is the code to fetch the values.
Regards
Karthik