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
RedmossSubC4iRedmossSubC4i 

AggregateResult - System.ListException: Row with null Id at index: 0

One of my aggregate result SOQL queries is getting the error, System.ListException: Row with null Id at index: 0.  If I'm correct this tells me that the query came up with no results.  What's confusing is that I have another aggregate result that is super similar that works just fine.  Can anyone please help explain how to fix this?  Thanks in advance!

Problem code:
//Aggreate SOQL to get next scheduled task
Map<Id,AggregateResult> LDRnextTaskSummary = new Map<Id,AggregateResult>([SELECT WhoId LeadId, MIN(CreatedDate) NextDate FROM Task WHERE (WhoId IN: leadIds) AND (Owner.Profile.Name LIKE '%LDR%') AND (Status = 'Not Started' OR Status = 'In Progress') AND (ActivityDate >= TODAY) GROUP BY WhoId]);

Similar SOQL That Works:
//Aggregate SOQL to get First and Last Tasks
  Map<Id,AggregateResult> LDRpastTaskSummary = new Map<Id,AggregateResult>([SELECT WhoId LeadId, MIN(CreatedDate) FirstDate, MAX(CreatedDate) LastDate FROM Task WHERE (WhoId IN :leadIds) AND (Owner.Profile.Name LIKE '%LDR%') AND (Status = 'Completed') GROUP BY WhoId]);

Was testing and this also got the same error:
Map<Id,AggregateResult> LDRnextTaskSummary = new Map<Id,AggregateResult>([SELECT WhoId LeadId, MIN(CreatedDate) NextDate FROM Task WHERE (WhoId IN: leadIds) AND (Owner.Profile.Name LIKE '%LDR%') GROUP BY WhoId]);
SaraHasNoLimitsSaraHasNoLimits
Have you tried running the code in the Execute Anoymous Window of the Developer Console. I am curious what it gives you there? Also, are you doing this in a Sandbox where the data is possibly not what you expect it to be?
RedmossSubC4iRedmossSubC4i
Yes, I'm doing this inside a sandbox where I have created a few test records for the query to capture.  I ran Execute Anonymous (Force.com IDE) on only the SOQL portion as a query inside a list and got the expected record count.  I was unable to run the aggregate result in its entirety since I won't be able to get the leadsIds set used in the IN statement.  I have this itching feeling that this is related to some knowledge I'm missing about either SOQL or Aggregate Results.  Seems so finnicky so far and I can't see a pattern here as to why only one of the examples above worked and the other two failed.
SaraHasNoLimitsSaraHasNoLimits
I understand. Like you say, I do not see any obvious reason why the first query would fail to return results, but the second one would unless there is a problem with the actual data. I am sorry to be no help to you. Good luck...