Another Salesforce blog!!!

Salesforce, Apex

Maps in Apex


MAP:

Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);
for(id myid : m.keyset()) {
     system.debug('///' + m.get(myid).LastName);
}
Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);
for(id myid : m.keyset()) {
     system.debug('///' + m.get(myid).LastName);
}
Map<id, id>	newRAEMap = new Map<Id, Id>();
newRAEMap.put(aAd_Board.id, aAd_Board.RAE__c);
for (Id key: newRAEMap.KeySet()){
if (oldRAEMap.get(key) != null ){ }
}

for (Id recordId : quotes.keySet())
{}

for (Id recordId : quotes.keySet())
{
    for (Quote quoteRecord : quotes.get(recordId))
    {
        // do something
    }
}

Alternatively if you want to go through all the quotes regardless of the IDs, you can do this:

for (List <Quote> quoteList : quotes.values())
{
    for (Quote quoteRecord : quoteList)
    {
        // do something
    }
}

Alternatively if you want to go through all the quotes regardless of the IDs, you can do this:

for (List <Quote> quoteList : quotes.values())
{
    for (Quote quoteRecord : quoteList)
    {
        // do something
    }
}

Once you have your data in a map you can also loop through it. But you need to take advantage of the keySet method on the map, which will return a set of keys. Here is an example of retrieving into a list, converting that list to a map and then looping through the map. –
See more at: https://www.sundoginteractive.com/blog/apex-why-maps-are-your-friend#sthash.6roAF2BI.dpuf

list myAccounts = new list(); 
myAccounts = [Select ID, Name from Account limit 10]; 

map myAMap = new map();
for ( Account a : myAccounts )
{ 
   myAMap.put(a.ID, a.Name); 
}  

for ( ID aID : myAMap.keySet() )
{ 
  system.debug(loggingLevel.debug, myAMap.get(aID)); 
}  

Leave a Reply

Your email address will not be published. Required fields are marked *

*