• Articles
  • Tutorials
  • Interview Questions

What is Collection in Salesforce?

What is Collection in Salesforce?

Table of content

Show More

Check out this Salesforce Training video for a better understanding

Video Thumbnail

What is Collection in Salesforce?

Collections in Salesforce are various types that can contain numerous records. In other words, collections are groups of records that are of a similar type. Collections have the ability to dynamically rise and shrink depending on the business needs. Collections in Apex can be lists, sets, or maps. There is no limit to the number of records that can be stored in a collection.

Companies previously used to have a difficult time managing records for various clients since there was no software that could manage customer information of thousands in one place. The situation improved with Salesforce collections. With Salesforce collections, companies started to organize customer records in one place, which helped in improving customer satisfaction.

Learn with our Salesforce tutorial thoroughly and improve your knowledge.

Types of Collections

Salesforce has three types of collections:

  • List collection
  • Set collection
  • Map collection

List Collection

A list can hold any type of data and is one of the most important types of collection. A list is an ordered collection of any data type such as primitive types, collections, sObjects, user-defined types, and built-in Apex types.

The following are the key features of a list collection in Salesforce:

  • Duplicates and nulls are allowed in a list collection.
  • “List” is the keyword to declare a list collection.
  • In a list collection, the index position of the first entry is always zero (0).
  • An Apex list collection has the ability to grow dynamically over time.
  • In a list collection, the data type can be both primitive and non-primitive.
  • The keyword followed by the data type has to be used within <> characters to define a list collection.

The syntax for a list is as follows:

List<datatype> listName = new List<datatype>();

List Class Salesforce:  All methods required for a list collection are stored in the Apex list class.

Become an expert in Salesforce with this industry-standard Salesforce training.

Salesforce Master Course

What are the Methods in List?

In order to use lists in programming to achieve certain functions, a few methods in Salesforce are available.

The following are the list-class methods in Salesforce:

  • Add: The values or items are added to the list using the list.add() method.

The syntax to use the “add” method is as follows:

List name.add();

Here is an example:

List<String> names =new List<String>();
names.add('Ram');

Here, we are adding the “Ram” value to the list “names” using the method “add”.

  • Clone: It is the method of making a new record by using the details of an existent one. Simply put, it is creating a duplicate record.

The syntax to use the “clone” method is as follows:

NewList= Old list name. clone();

Here is an example:

New Names= names.clone();

In the above example, a new list “New Names” is being created, which is the clone of the list “names”.

  • Remove: It is the method of removing the specific value of the list.

The syntax to use the “remove” method is as follows:

List Name.remove(index);

Here is an example:

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

names.add('Ram');

names.add('John');

names.add('Sony');

names.remove(2);

By using “remove” syntax in the above-mentioned example, the second position value, which is “Sony”, is being removed.

Are you preparing for an interview in Salesforce, take a look at these Salesforce interview questions.

  • Size: It is the method of finding the number of elements present in the list.

The syntax to use the “size” method is as follows:

Listname. size();

You have to write the list name of which you want to find the size.

  • Equals: This is the method of defining whether the value is equal or not. If the list and the specific list are equal, true is returned; otherwise, false is returned.

The syntax to use the “equals” method is as follows:

result=Listone.equals(Listtwo);

Here, “List two” is being compared with “Listone”. If both elements are the same, it shows true; if both elements are not the same, then it shows false.

  • Get: This is the method that helps to return or find out a value from a list.

The syntax to use the “get” method is as follows:

String getlist= list.get(index);

In this syntax, you have to replace “list” with “list name” and “index” with “index number”.

  • Set: This is the method that is used to change the element or value of an index.

The syntax to use the “set” method is as follows:

list.set(index, value);

The value of the index, which is mentioned in the code, will be changed to the value that is present in the code.

For example, if you write names.set(1, Mark);

Here the value at the “index 1” of the list of “names” will be changed to “mark”.

  • Clear: This is the method that removes or deletes an element in the list.

The syntax to use the “clear” method is as follows:

list.clear();

These are a few popular methods. We have discussed Apex collections in Salesforce; now, we are going to discuss the next type of collection.

Get 100% Hike!

Master Most in Demand Skills Now!

Set Collection

Set is an unordered collection. It is a unique set of values that do not have any duplicates. If you want no duplicates in your collection, then you should opt for this collection.

The following are the key features of a set collection in Salesforce:

  • Any data types, such as primitive data types and sObjects, are allowed in a set collection.
  • A set collection does not have an index.
  • A set collection does not contain any duplicates or null values.
  • Set collections are not widely used in Salesforce Apex.
  • Sets can hold collections that are nested inside of one another.
  • You have to use the set keyword followed by the primitive data type name within <> characters to declare a set collection.

The syntax for a set is as follows:

Set<datatype> setName = new Set<datatype>();

Preparing for a Salesforce Interview! Check out our Salesforce Admin Interview Questions.

What are the Methods in a Set?

The following are the set-class methods in Salesforce:

  • Add(): Use the add method to add an element in a set collection.

The syntax for the “add()” method is as follows:

setName.add();

Here is an example:

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

CitiesSet.add(Newyork); // Newyork will be added in the cities set.

  • Remove(): This method is used to remove element(s) from a set collection.

The syntax for the “remove()” method is as follows:

SetName.remove();

Here is an example:

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

CitiesSet.remove(Newyork); //Newyork will be removed from the cities set.

  • Size(): Same as in list collections, the size method can be used to find the count of elements in a set.

The syntax for the “size()” method is as follows:

Setname. size();

  • Contain(): This method is used to determine whether or not an element exists in a set or not.

The syntax for the “contain()” method is as follows:

Setname.contain(element);

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

The syntax for the “isEmpty()” method is as follows:

Setname.isEmpty();

These are some of the methods in a set collection. There are many other methods available in a set collection such as clear(), clone(), hashcode(), equals(), and many more.

Map Collection

Map is a key-value pair that contains each value’s unique key. It is used when something needs to be located quickly.

The following are the key features of a map collection in Salesforce:

  • Any data type can be used for both the key and the value in a map collection.
  • In a map collection, the null value can be stored in a map key.
  • The keys of type string are case-sensitive in a map collection.
  • You have to use the map keyword followed by the key and value data types enclosed in <> to declare a map collection.

The syntax for a map is as follows:

Map<datatype_key,datatype_value> mapName = new Map<datatype_key,datatype_value>();

Want to get certified in Salesforce? Here is the Salesforce Training in Bangalore you are looking for!

What are the Methods in Map?

The following are the map-class methods in Salesforce:

  • put(key, value):  This method is used to add a value to a map collection.

The syntax for the “put(key, value)” method is as follows:

Map<integer, string> World Map = new Map <integer, string>();

World Map.put(1, ‘Australia’);

  • get(key): This method helps to find the value of a key in a map collection.

The syntax for the “get(key)” is as follows:

Datatype_value = mapName.get(key);

  • containskey(key): This method helps to declare that a map contains a specific key.

The syntax for the “containskey(key)” is as follows:

mapName.containskey(key);

  • keySet(): This method is used to return a set containing all of a map’s keys.

The syntax for the “keySet()” is as follows:

Set<datatype_key> = mapName.keySet();

Check out our blog to learn about map class in salesforce!

Conclusion

By now, you must have understood what exactly are collections in Salesforce with examples and how these collections can be used.

Course Schedule

Name Date Details
Salesforce Certification 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Salesforce Certified Professional

Rahul is a seasoned Salesforce Certified administrator and app builder with 10+ years of experience in many Salesforce technologies, such as Salesforce CRM and business process automation. In his free time, he likes to write and read about the latest technologies.