Easy way to IT Job

Collection in Salesforce
Share on your Social Media

Collection in Salesforce

Published On: June 7, 2022

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.

List 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.

Syntax

List name.add();

Example

List <String> names = new List<String>();

names.add(‘Felix’);

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.

Syntax

NewList = Old list_name.clone();

Example

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

Syntax

List Name.remove(index);

Example

List<String> names = new List<String>();

names.add(‘Felix’);

names.add(‘Rohan’);

names.add(‘Jonah’);

names.remove(2);

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.

Syntax

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.

Syntax

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.

Syntax

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

Syntax

List.set(index, value); //index value will be changed to the value

Clear: This is used to remove an element in the list

Syntax

List.clear();

Set Collection

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.

Syntax

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.

Syntax

setName.add();

Example

Set <string> CountriesSet = new set <string>();

CountriesSet.add(India);

Remove: It is used to delete the element(s) from a set collection using the remove() method.

Syntax

SetName.remove();

Example

Set <string> CountriesSet = new set <string>();

CountriesSet.remove(India);

Size: It is used to find the number of elements in a set collection using the size() method.

Syntax

Setname.size();

Contain: It is used to check whether the specific element present or not in a set collection using contain() method.

Syntax

Setname.contain(element);

isEmpty: It is used to find if the element is zero or not using isEmpty() method.

Syntax

Setname.isEmpty();

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.

Map Collection

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.

Syntax

Map<datatype_key, datatype_value>

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

Syntax

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

Syntax

Datatype_value = mapName.get(key);

Containskey(key): It is used to declare a map that contains a specific key

Syntax

mapName.containskey(key);

KeySet(): It is used to return a set containing all of the map’s keys.

Syntax

Set<datatype_key> = mapName.keySet();

Conclusion

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.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.