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
Jeffrey ZhangJeffrey Zhang 

getting System.ListException: Row with null Id at index 0 error with my Map AggregateResult

Not too familiar with maps and aggregateResult in SF, so I'm not sure if this is a formatting issue or something else, but I keep getting that error in my trigger when this is called.
Map<Id,aggregateResult> maxDates = new Map<id,AggregateResult>([Select whoid id, Max(Sales_Activity_Date__c) myMax, Min(Sales_Activity_Date__c) myMin from Task 
         where whoid = :who and status = 'Completed' and whoid!=NULL GROUP BY whoid]);

Is it a formatting issue, or something else?

Thanks.
Best Answer chosen by Jeffrey Zhang
Alain CabonAlain Cabon
Try this : 
 
Map<Id,AggregateResult> results = new Map<id,AggregateResult>([SELECT AccountId id, COUNT(Id) ContactCount FROM Contact WHERE AccountId != NULL GROUP BY AccountId]);

... Error. Caramba!

and now try this: 
 
Map<Id,AggregateResult> results = new Map<id,AggregateResult>([SELECT AccountId Id, COUNT(Id) ContactCount FROM Contact WHERE AccountId != NULL GROUP BY AccountId]);

... it works fine!!

Bizarre but Jeffrey was right excepted for the "id".

Alain

All Answers

Alain CabonAlain Cabon
Hello Jeffrey,

Try replacing; Select whoid id,    by Select whoid Id,  
(uppercase for the "I")

Regards
Alain
GarryPGarryP
This does not seems to be a valid query. As AggregateResult is a read-only sObject and is only used for query results not sure if this is the right way to use it.
Why cant we use the List Agregate result and iterate over it to get the desired results?
something like this should work

<pre>
for(AggregateResult maxDatesAggregateResult:[Select whoid id, Max(Sales_Activity_Date__c) myMax, Min(Sales_Activity_Date__c) myMin
              from Task
              where whoid = :who
              and status = 'Completed' and whoid!=NULL
              GROUP BY whoid]){
 String id = String.valuesOf(maxDatesAggregateResult.get('id'));           
 Double myMax = Double.valuesOf(maxDatesAggregateResult.get('myMax'));           
 Double myMin = Double.valuesOf(maxDatesAggregateResult.get('myMin'));           
 //do you logic here
}
</pre>
also the code is being used in Trigger, so you should use the trigger ID that is in context. In this case i am assuming :who is list of Ids that are created from trigger that is in context
 
Alain CabonAlain Cabon
@Girish: I though the same thing at first because it is case insensitive but ...
 
Map<Id,aggregateResult> maxDates = new Map<id,AggregateResult>([Select whoid Id, Max(CallDurationInSeconds) myMax, Min(CallDurationInSeconds) myMin from Task 
         where whoid!=NULL GROUP BY whoid]);
... works perfectly !!  Just change "id" with "Id".

Alain
Alain CabonAlain Cabon
Try this : 
 
Map<Id,AggregateResult> results = new Map<id,AggregateResult>([SELECT AccountId id, COUNT(Id) ContactCount FROM Contact WHERE AccountId != NULL GROUP BY AccountId]);

... Error. Caramba!

and now try this: 
 
Map<Id,AggregateResult> results = new Map<id,AggregateResult>([SELECT AccountId Id, COUNT(Id) ContactCount FROM Contact WHERE AccountId != NULL GROUP BY AccountId]);

... it works fine!!

Bizarre but Jeffrey was right excepted for the "id".

Alain
This was selected as the best answer
GarryPGarryP
@Alain, Great! sometimes we really needs a bird eye! :)
cheers!
Jeffrey ZhangJeffrey Zhang
wow that worked! Thanks for both of y'all.

It's always the littlest of things that causes the biggest headaches.
WminWmin
and you though this conversation was over.

I have:

Map<Id, AggregateResult> lastY = new Map<Id, AggregateResult>([select Line__c, sum(number_of_calls__c) totalcalls from detailed_call_data__c where (date__c >= :dateStart.addYears(-1)) and (date__c <= :dateEnd.addYears(-1)) and Line__c != '' group by Line__c]);

and getting the same error. Any ideas anyone?
vivek saxena 20vivek saxena 20
What i do ?:  

Map<Id, AggregateResult> m = new Map<Id, AggregateResult>([Select AccountId, Count(Name) from Contact GROUP BY AccountId]); 

                                            User-added image