Background

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

Questions

  1. Why?
  2. What am I doing wrong?
  3. How can I fix it?
up vote 9 down vote accepted

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

SELECT
    OpportunityLineItemId Id, 
    MIN(ScheduleDate) Starts
FROM OpportunityLineItemSchedule 
WHERE ...
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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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