I'm working on building a new trigger that for some reason has me a bit stumped on the best way to query some related data.
I have an array of recently updated opportunities:
public void OnAfterUpdate(Opportunity[] updatedOpportunities) {
//How do I build a Map of related opportunity line items?
//lineItems = someMap.get(someOpportunityId);
}
In order to avoid hitting any governor limits I'd like to get all of the related opportunity line items, but, in a way that they are easily retrieved based on the opportunity id.
I was originally thinking of doing something like:
opportunityLineItemsMap = new Map<Id, List<OpportunityLineItem>>([SELECT OpportunityId, PricebookEntry.Product2.Name FROM OpportunityLineItem]);
However, this obviously isn't valid syntax. Is there an easy way to build a map like this or am I stuck with a nested loop of iterating over each opportunity and then over each line item to build the data structure myself?