Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writting a code at Opportunity's before insert trigger. There, I want to create a Map of Account's related to all oportunities indexed by theirs opportunity ID. I expect that each Map entry to be something like this: Key=OpportunityID; Value=Account Object. Could someone help me with the aprpriate statement to achieve it? I'm suppose this could be acomplished using a soql statement in the Map initialization.

Best regards,


share|improve this question
up vote 5 down vote accepted

Here is one of the answers, and you can always make it better

// It's the map you want
Map<Id,Account> mapO_A = new Map<Id,Account>();

// First of all, get the opportunities from database
Map<Id,Opportunity> mapOO = new Map<Id,Opportunity>([SELECT Id,AccountId FROM Opportunity]);

// Now let's get the accountId
Set<Id> accountId = new Set<Id>();
for(Opportunity op : mapOO.values()){

// Get the account you want
Map<Id,Account> mapAA = new Map<Id,Account>([SELECT Id,Name FROM Account WHERE Id IN :accountId]);

// Put them together
for(Id opId : mapOO.keySet()){
    Id acctId = mapOO.get(opId).AccountId;

// Done
share|improve this answer

The map constructor can only sort by the object ID of the list<object> you pass to it. (This is what you actually do when you pass on a soql query)

Map<Id,Account> map = new Map<Id,Account>([select id, name from account]);

The map you're looking for will need to be done in Apex

1) compose a map< account Id, opportunity Id> by iterating your collection

2) query the accounts based on the keyset

3) iterate your result and fill a map<opportunity Id, account ID> by looking up the opportunity Id from the first map.

share|improve this answer

In Apex, you can create any sort of map. The first thing to consider is the structure of a map / how it works. A map contains two elements: the keyset and the values list.
The keyset, as the name implies, is a set -- therefore it stores no duplicate values. The map's values is a list -- so it can have duplicate values.

To create a map, first you need to construct it, so let's create a fresh new map to work with:

map<string,integer> myFirstMap = new map<string,integer>();

Now, we need to put elements into the map. By the way we defined our map, it takes strings as its input and gives integers as its output. So, let's give our map some elements. Use the put method to insert elements into the map.


Now to get the value, use the get method of the map.

integer a_value = myFirstMap.get('a');   


To access the map's keyset and values, use the keyset() and values() methods, respectively.

set<string> theKeyset = myFirstMap.keyset();
list<integer> theValues = myFirstMap.values();

And that's pretty much it for the map methods. The big thing to consider is that the keyset is a set. What if you do the following?

myFirstMap.put('hey there',88);
myFirstMap.put('hey there',420);

system.assertEquals(myFirstMap.get('hey there', xxxx );  //  what's xxxx?
// answer:  xxxx = 420

The map will store only the last value given for a specific key. Another consideration is "What happens if you try to get a key that has never been set?"

system.assertEquals(myFirstMap.get('this key has never been set X_X'),null);

Now to specifically address your question: Suppose we want to get a list<Account> via SOQL query and then create a map with the Account's OpportunityID as its key.

list<Account>  accountList = [SELECT id,OpportunityID
                              FROM Account
                              WHERE OpportunityID!=null];

map<id,Account> oppIdAcctMap = new map<id,Account>();

for(Account a:accountList)


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.