Blog Blog Posts Business Management Process Analysis

What is Collection in Salesforce?

This blog focuses on the following concepts:

Check out this Salesforce beginner tutorial video for better understanding

 

What is Collection in Salesforce?

Collections in Salesforce are variable 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.

 

Types of Collections

Salesforce has three types of collections:

Learn Salesforce thoroughly and improve your knowledge with Intellipaat’s blog.

 

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:

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 Apex list class.

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

 

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:

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 “Ram” value to the list “names” using the method “add”.

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

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.

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.

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

result=Listone.equals(Listtwo);

Here, “Listtwo” 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.

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

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

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.

 

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:

The syntax for a set is as follows:

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

What are the Methods in Set?

The following are the set-class methods in Salesforce:

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.

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.

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

Setname. size();

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

Setname.contain(element);

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:

The syntax for a map is as follows:

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

What are the Methods in Map?

The following are the map-class methods in Salesforce:

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’);

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

Datatype_value = mapName.get(key);

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

mapName.containskey(key);

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

Set<datatype_key>  = mapName.keySet();

By now, you must have understood what exactly are collections in Salesforce with examples and how these collections can be used. If you still have any query, feel free to ask on our Salesforce community page.

The post What is Collection in Salesforce? appeared first on Intellipaat Blog.

Blog: Intellipaat - Blog

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="https://www.businessprocessincubator.com/content/what-is-collection-in-salesforce/?feed=html" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples

BPMN.org

XPDL.org

×