6

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?

1 Answer 1

12

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .