Sunday, 8 May 2016

How to add list of records to a map?

We all know map is very much required in SFDC, As a developer we should be familiar with map uses. This post is all about to add list of records to a map.

Map<Id,Account> account_map =  new Map<Id,Account>();
public static void addinglisttoMap(List<Account> accList){

for(Account acc:accList){
account_map.put(acc.id,acc);
}

Normally we used to do like above, where processing time will be more(CPU execution time will be more) because we are iterating over a for loop to construct a map. 

I will tell you the best way to construct the map where we can avoid iteration. 

public static void addinglisttoMap(List<Account> accList){

Map<Id,Account> account_map =  new Map<Id,Account>(accList);
}

Advantages- 
Processing time will be very less
No of character in apex class is also less. 

Hope it will be helpful. 

Keep coding and exploring :)  

Order Of Execution In Salesforce