You need to sign in to do that
Don't have an account?
Visithra
Help me with code, created aggregateresult into map to count number of records with rating grouped., I Got stucked while traversing Loop
Map<ID, AggregateResult> accountMap=new Map<ID, AggregateResult>([Select
count(ID) ratingCount, Rating
From Account
Group by Rating
]);
for(AggregateResult accRecord:accountMap)
{
system.debug('ratingCount');
system.debug(accRecord.get('Rating'));
}
Getting error as:: Line: 6, Column: 1
Loop must iterate over collection: Map<Id,AggregateResult>
count(ID) ratingCount, Rating
From Account
Group by Rating
]);
for(AggregateResult accRecord:accountMap)
{
system.debug('ratingCount');
system.debug(accRecord.get('Rating'));
}
Getting error as:: Line: 6, Column: 1
Loop must iterate over collection: Map<Id,AggregateResult>
Hi visi,
You have to iterate on Map values i.e.,
Map<ID, AggregateResult> accountMap=new Map<ID, AggregateResult>([Select
count(ID) ratingCount, Rating
From Account
Group by Rating
]);
for(AggregateResult accRecord:accountMap.values())
{
system.debug('ratingCount');
system.debug(accRecord.get('Rating'));
}
Please mark this as the best answer if this helps.
To avoid this error Please Use a list instead of a map.
This will give you the count and the rating.
List< AggregateResult> accountMap=new List< AggregateResult> ([Select
count(ID) ratingCount, Rating
From Account
Group by Rating
]);
for(AggregateResult accRecord:accountMap)
{
system.debug('ratingCount'+accRecord.get('ratingCount'));
system.debug(accRecord.get('Rating'));
}
https://salesforce.stackexchange.com/questions/284260/fatal-errorsystem-listexception-row-with-null-id-at-index-0
https://salesforce.stackexchange.com/questions/327000/assign-map-keys-and-values-to-account
https://salesforce.stackexchange.com/questions/160220/compile-error-loop-must-iterate-over-a-collection-type-mapid-listevent