
Collections in Salesforce - All you Need to Know
There are basically 3 types of collections used in Apex i.e.
- List
- Set
- Map
List
- List is a collection of ordered elements.
- This means, each and every element’s place is fixed or we can tell where an element will be, by using an index number.
- Duplicate values are allowed in the List collection.
- List is the return type of any SOQL query, hence List is very widely used for processing SOQL results.
- Every element in the list has an index that is numbering which starts with zero.
- List can store any type of data.
Set
- Set is a collection of unordered elements which don’t have any index number.
- We can never tell values are present in a set at what location.
- Set cannot contain duplicate values. If we add a duplicate value by mistake, it will neglect that, no issues.
- It can store any primitive data type like integer, string or any user-defined data type like sObject.
- We use sets in a situation where we want the data to be strictly unique.
Don't forget to check out: Campaigns In Salesforce - All You Need to Know
Map
- Map is the most powerful type of collection.
- It works in pairs of elements, never as a single value like List and Set.
- Map has pairs in the format of key and value.
- Keys cannot be duplicate like in Sets.
- Values can be duplicate like in Lists.
- We can have keys or values of any data type ex- string, integer etc.
- Maps are specially used when we can not refer records easily using 0,1,2,3,4…such meaningless numbers which have no connection to the business process in the list. We need something which is related to business to refer to the values. Hence we use Maps rather than lists in such cases.
Use in Apex
Below is the code for understanding the usage of collections in Apex:
Trigger Functionality: This trigger is used to update the city field in the account object with the city field in its related opportunity.
Handler Class:
public class AccountCityUpdateHandler { public static void CityUpdateMethod(List<opportunity> opplist){ //created a list of opportunity list<account> acclist= new list<account>(); //created a list for accounts set<id> accidset= new set<id>(); //created a set for account ids map<id,opportunity> MapAccidOpp= new map<id,opportunity>(); //created a map for id and opportunity for(opportunity opp: opplist){ //applied for loop if(opp.Accountid!=null){ //checking if account id is null or not in the opportunity record accidset.add(opp.AccountId); //if not null then the id is stored in account ids set } } for(opportunity opp: opplist){ //applied for loop MapAccidOpp.put(opp.AccountId,opp); //added opportunity id and opp in the map } list<account> accountlist=[select id from account where id IN:accidset]; //used query to call account ids from account id set for(account acc: accountlist){ //applied for loop if(MapAccidOpp.get(acc.Id).City__c!=null){ //checking if city field in opportunity record is null or not acc.City__c= MapAccidOpp.get(acc.Id).City__c; //if not null then account city name is updated acclist.add(acc); //updated record is added in the account list } } if(acclist!=null && acclist.size()>0){ //null check and size check on account list update acclist; //if not null then account list is updated } } }
Check out another amazing blog by Romil here: Types of Relationships in Salesforce - All You Need to Know
Trigger:
trigger AccountCityUpdateTrigger on Opportunity (after insert, after update) { if(Trigger.isinsert || Trigger.isupdate){ AccountCityUpdateHandler.CityUpdateMethod(trigger.new); } }
Responses