I'm writting a code at Opportunity's before insert trigger. There, I want to create a Map of Account's related to all Trigger.new 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,



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

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 trigger.new 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.


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)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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