Collection in Salesforce
Salesforce is a widely used CRM tool useful for support, sales, and marketing teams by allowing businesses to utilize cloud technology to better connect with customers, partners, and team members.
Salesforce collections are the group of records that are of the same kind. The collection in Salesforce consists of various types of records and it can rise or shrink dynamically as per the business requirements.
Salesforce collections might be as lists, sets, or maps and there will be no limit to the number of records to be stored in a collection. Salesforce collection is used in top companies to organize their customer records in one place for improving customer satisfaction.
Three Major Types of Collection
There are three major types in Salesforce Collection and they are List Collection, Set Collection, and Map Collection.
A list contains any type of Data and it will be an ordered collection that consists of any type like primitive types, sObjects, collections, built-in Apex types, or user-defined types. Below are the major features of a list collection in Salesforce
- Duplicates and nulls are allowed
- “List” keyword will be used to declare a list collection
- The index position of the first entry must be zero always.
- Ability to grow dynamically with an Apex list collection
- Both primitive and non-primitive data types are allowed
- To define a list collection, <> characters will be used in the keyword followed by the data type.
The syntax for a list collection in Salesforce
List<data type> listname = new List<datatype>();
Apex List Class contains all methods needed for a list collection.
Methods in List Collection
Salesforce includes certain methods that can be utilized in List Collection to achieve some functions. Some of the list-class methods in Salesforce are listed below
Add: This is used to add the values or items in the list using the list.add() method.
List <String> names = new List<String>();
In this example, the value ‘Felix’ is added to the list of “names” using the “add” method.
Clone: This is used to create a new record with the details of an existing one. It is all about creating a duplicate record of the existing record.
NewList = Old list_name.clone();
New names = names.clone();
In this example, the new list called “New Names” is created to duplicate the “names” list that is existed
Remove: This is used to delete the particular value of the list
List<String> names = new List<String>();
In this example, the second positioned value which is ‘Jonah’ will be removed.
Size: This is used to indicate the number of elements in the list.
Listname.size(); //list name should be mentioned in the example
Equals: This is used to define whether the value is equal or not. True will be returned if the list and the mentioned list are equal or it will return false.
result = Listone.equals(Listtwo);
In this example, list one and list two are compared. If both elements are the same, it sends true. If both elements are not equal, it sends a false.
Get: This is used to find out a value from a list.
String getlist = list.get(index); //list name and index number should be given
Set: This is used when we want to make changes in the element or value of an index
List.set(index, value); //index value will be changed to the value
Clear: This is used to remove an element in the list
Set is the collection that contains unordered elements. It can’t be duplicated as it is as a unique set of values. Below are the specialized features of set collection in Salesforce.
- Any data types like primitive or sObjects are allowed
- Set doesn’t have an index
- Set doesn’t have null or duplicate values
- Set can contain nested collections
- Used widely in Salesforce Apex
- To declare a set collection, the ‘Set’ keyword will be used followed by the primitive data type name within <> characters.
Set<datatype> setName = new Set<datatype>();
Popular methods in Set
Following are the methods used in Set Collection
Add: It is used to add an element in a set collection using add() method.
Set <string> CountriesSet = new set <string>();
Remove: It is used to delete the element(s) from a set collection using the remove() method.
Set <string> CountriesSet = new set <string>();
Size: It is used to find the number of elements in a set collection using the size() method.
Contain: It is used to check whether the specific element present or not in a set collection using contain() method.
isEmpty: It is used to find if the element is zero or not using isEmpty() method.
Apart from these above-mentioned methods, there are some other methods popular in the set collection and they are clone(), clear(), equals(), hashcode(), and so on. They are similar to list collections in Salesforce.
The map is a key-value pair that consists of each element’s unique key and it is used to locate something that is needed instantly. Below are the features of Map Collection in Salesforce
- Any data type can be used for both key and the value
- Map key can contains null value.
- The type string keys should be case-sensitive
- To declare a map collection, the map keyword will be used followed by value the key and value data types with <> characters.
mapName = new Map<datatype_key, datatype_value>();
Popular methods used in Map Collection
Following are the methods used in map collection in salesforce.
Put(key, value): It is used to add a value to a map collection
Map<integer, string> World Map = new Map<integer, string>();
World Map.put(1, ‘Europe’);
Get(key): It is used to find the value of a key in a map collection
Datatype_value = mapName.get(key);
Containskey(key): It is used to declare a map that contains a specific key
KeySet(): It is used to return a set containing all of the map’s keys.
Set<datatype_key> = mapName.keySet();
Collections in “Salesforce” are a group of data types that allow the developer to aggregate, collect, or multiple other types in a single location.
The learning of collections like List, Set, and Map is used to manage the limits when querying records in Salesforce. Enroll in our Salesforce Training in Chennai to gain expertise in the Salesforce platform.