Is it possible to run a SOQL Query and get a MAP returned?

It is possible to run query and the results inserted into an Apex list:

List<Opportunity> opportunities = [SELECT Opportunity.OwnerId,
                                          Opportunity.Probability,  
                                          Owner.Name FROM Opportunity 
                                    WHERE Opportunity.LastModifiedDate = LAST_N_DAYS:7];

Is it possible to return a Map? Where the key would be the OpportunityID and the value the Opportunity?

If not, what is the quickest way to convert to a map?

Answer

I know only one:

Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);

Here is the doc: Maps of sObjects

Attribution
Source : Link , Question Author : dublintech , Answer Author : Sergej Utko

Leave a Comment