126

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?

fred
  • 3,335
  • 3
  • 16
  • 27
dublintech
  • 4,253
  • 10
  • 40
  • 56

2 Answers2

189

I know only one:

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

Here is the doc: Maps of sObjects

Sergej Utko
  • 22,020
  • 11
  • 59
  • 88
  • 3
    That's a much better way to do it. I updated your link since it was just taking me to the home page instead of the specific topic. – Ryan Elkins Dec 11 '12 at 18:01
  • 4
    This is one to commit to memory. I use it all the time. – Kevin O'Hara Dec 11 '12 at 18:47
  • 2
    Also, a quick way to get a set of returned Ids from a query is similar.. using the above code, just do Set myIds = m.keySet(); – James Loghry Dec 11 '12 at 19:15
  • 15
    To clarify, this method can only be used to generate Maps using Id of the object you are querying as the key. If you want to use a different value as the key, you will have to iterate over the list returned by your query and put values into a Map. For instance, if you wanted to use AccountId as the key, you would need to do something like this: List<Opportunity> oppList = [Select Id, AccountId from Opportunity]; Map<Id,Opportunity> accOppMap = new Map<Id,Opportunity>(); for(Opportunity o : oppList){ accOppMap.put(o.AccountId,o); } – Todd Sprinkel May 04 '15 at 15:13
  • @mast0r link is again changed :) – C0DEPirate Sep 16 '15 at 07:13
  • any option to use it with child query like this.?

    Map<AccountId, List> m = new Map<AccountId, List>([SELECT Id (SELECT Id FROM Contacts) FROM Account]);

    – Nitish Kulkarni Dec 16 '20 at 13:45
  • I was getting this result can you clarify why i'm getting isocurrecyfireld here this is my Query--> map<id,Account> acc = new map<id,account>([select id,name,CreatedDate from account limit 10]); system.debug(acc);

    result--> 0016F00002THXyjQAH=Account:{Id=0016F00002THXyjQAH, Name=test1, CreatedDate=2018-09-03 12:33:32, CurrencyIsoCode=USD}

    – Shakti Jadon Apr 06 '21 at 20:04
  • The documentation suggests that something like this is possible: Map<String, Contact> m = new Map<String, Contact>([SELECT LastName, Email FROM Contact]) and it will execute, but the key of the map will be filled with the Id, and not the LastName, of the contact. – Sander de Jong Feb 15 '23 at 13:54
65

Alternatively, if you have a list already -- say you're in a situation where you need a map, but aren't in a mood to refactor your entire class to handle a map, you can convert your list of results to a map thusly:

Map<Id,OBJ_TYPE> mapFromList = new Map<Id,OBJ_TYPE>(List_object_variable);

This will generate a map from the list as if you'd queried directly into a map.

Kevin P
  • 7,208
  • 2
  • 27
  • 40