I have this Apex SOQL query:

    myMap = new Map<Id, AggregateResult>([SELECT OpportunityLineItemId, 
                                        MIN(ScheduleDate) Starts
                                        FROM OpportunityLineItemSchedule 
                                        WHERE OpportunityLineItemId IN :opportunityLineItemMap.keySet()
                                        AND ScheduleDate != NULL
                                        GROUP BY OpportunityLineItemId]);

Which throws this error:

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


  1. Why?
  2. What am I doing wrong?
  3. How can I fix it?

You need to alias some field to Id in order for the Map<Id, SObject>(List<SObject>) constructor to work.

    OpportunityLineItemId Id, 
    MIN(ScheduleDate) Starts
FROM OpportunityLineItemSchedule 
GROUP BY OpportunityLineItemId

As your query is currently written, a given AggregateResult will only allow you to get three "fields":

  • expr0
  • OpportujnityLineItemId
  • Starts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.