function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
iKnowSFDCiKnowSFDC 

Problem with Aggregate SOQL Query

I'm trying to count a group of records and get the total records by account and then put the account id and the number of child records into a map.  I keep getting an "incompatible key type for map" on compile and I'm not sure why. Hopeing somone has an idea!

 

If I take the map out and just look at the debug, it's pulling out the Account id as  a string...

 

Map<String, Integer> acctCalls = new Map<String, Integer>();

List<AggregateResult> r = [SELECT Account_vod__r.Name acctid, COUNT(id) totalCalls
                           FROM Call2_vod__c  
                           GROUP BY Account_vod__r.Name];
for(AggregateResult i : r){
    acctCalls.put(i.get('acctid'), i.get('totalCalls'));

    system.debug(' account>>> ' + i.get('acctid') + 'number of calls>>> '+i.get('totalCalls'));
}                         
  

 Thanks,

JoAnn

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
Map<String, Integer> acctCalls = new Map<String, Integer>();

List<AggregateResult> r = [SELECT Account_vod__r.Name acctid, COUNT(id) totalCalls
                           FROM Call2_vod__c  
                           GROUP BY Account_vod__r.Name];
for(AggregateResult i : r)
{
    //acctCalls.put(i.get('acctid'), i.get('totalCalls'));

    acctCalls.put(String.valueOf(i.get('acctId')), Integer.valueOf(i.get('totalCalls')));
    system.debug(' account>>> ' + i.get('acctid') + 'number of calls>>> '+i.get('totalCalls'));
}  

 Try this.

All Answers

Naidu PothiniNaidu Pothini
Map<String, Integer> acctCalls = new Map<String, Integer>();

List<AggregateResult> r = [SELECT Account_vod__r.Name acctid, COUNT(id) totalCalls
                           FROM Call2_vod__c  
                           GROUP BY Account_vod__r.Name];
for(AggregateResult i : r)
{
    //acctCalls.put(i.get('acctid'), i.get('totalCalls'));

    acctCalls.put(String.valueOf(i.get('acctId')), Integer.valueOf(i.get('totalCalls')));
    system.debug(' account>>> ' + i.get('acctid') + 'number of calls>>> '+i.get('totalCalls'));
}  

 Try this.

This was selected as the best answer
iKnowSFDCiKnowSFDC

Thanks! I got so wrapped up in the key type that I couldn't see the forest for the tress on this one.  Your suggestion worked perfectly!

 

JoAnn