127

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?

3 Answers 3

192

I know only one:

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

Here is the doc: Maps of sObjects

8
  • 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. Commented Dec 11, 2012 at 18:01
  • 4
    This is one to commit to memory. I use it all the time. Commented Dec 11, 2012 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<Id> myIds = m.keySet(); Commented Dec 11, 2012 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); } Commented May 4, 2015 at 15:13
  • @mast0r link is again changed :)
    – C0DEPirate
    Commented Sep 16, 2015 at 7:13
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.

1
  • 1
    that is a good tip.
    – SeanW
    Commented Aug 6, 2021 at 18:00
-1

You can also try this if you are using global variable for map

map<id,Opportunity> mmap = new map<id, opportunity>();
list<opportunity> opplist = [select id,ReferralCustomer__c from Opportunity where ReferralCustomer__c != null limit 10];

mmap.putAll(new Map<Id, Opportunity>(opplist));
7
  • This is the same as both of the existing answers from 10+ years ago, but with less information and more steps. The intermediate list you're using to store the results of the query is not required.
    – Derek F
    Commented Apr 4 at 11:45
  • no,it is different as we are using global variable in it and you are correct we don't need a list but it is a simplified way of expressing the technicality. you can directly pass the query into the map instance.
    – Ajay Gupta
    Commented Apr 6 at 11:08
  • we are not using any looping over here as well \
    – Ajay Gupta
    Commented Apr 6 at 11:10
  • The global variable distinction doesn't make any difference here (and global should generally be avoided unless you know it's required). Neither of the existing answers use loops.
    – Derek F
    Commented Apr 6 at 12:30
  • I have that requirement where we need to use the map data in another method which needs a global variable to perform this.
    – Ajay Gupta
    Commented Apr 7 at 14:47

You must log in to answer this question.

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