Table of Contents
Introduction
A collection is a data structure(Data Types) that is used to store multiple values of the same data type. Collections are used to manipulate large sets of data efficiently and effectively.
If we have to store multiple values of the same type then if we do not use collection. Then here is how we will store the variables
String name;
String name1;
String name2;
String name3;
..
..
..
..
..
..
String nameN;
String fruit = ‘Apple’;
String fruitName = ‘Banana’;
Types of Collection
There are mainly three types of collection in Salesforce
- List
- Set
- Map
List Collection in Salesforce
A list is a collection of elements that are ordered and can be accessed using their index. The elements of a list can be of any data type, including primitive and non-primitive data types.
In Salesforce, lists are represented by the “List” keyword in Apex code and are commonly used to store and manipulate large data sets.
There is a class named List.
List<Data Type> variabname = new List<Data Type>();
Lists allow for various operations such as adding or removing elements, iterating over elements, searching for elements, and sorting elements.
List of Index = Total Size of List(Total Elements in List) – 1
Total Index = 4
First Index = 0
Last Index = 4-1
Hands-On
// Syntax
#1 -
List variabname = new List();
// Declare & Memory Allocation
List fruitList = new List(); // Size = 0, Size = 1
// Declare & Memory Allocation & Value Assignment
#2 -
List fruitList = new List{'apple', 'banana', 'orange'};
List orderStatusList = new List{
'Created','Submit','In Process',
'Out for Delivery','Delivered',
'Return Requested','Return Pickup Scheduled',
'Retured', 'Refund Requested',
'Refunded'
};
List numbersList = new List{1,2,4,5,3};
// Declare the variable
#3 -
List fruitList; // null
fruitList = new List(); // Memory Allocation
fruitList = new List{'apple', 'banana', 'orange'}; // Memory Allocation & Value Assignment
fruitList1 = new List{'apple', 'banana', 'orange'};
Methods of List in Salesforce
There are numerous methods of List class in Salesforce, however, there are some that are being used widely by the developers
#1-
List<Data Type> variabname = new List<Data Type>();
- add(element)
- set(index, element) – 1, ‘banana’
- get(index) – 0, 1, 2
- clear()
- clone()
- addAll(another set or list)
- remove(index)
- isEmpty()
- size()
- sort()
- Etc
For more information refer to this link – https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm
When to Use List
- Ordering the element is important
- Need of accessing the element by indexes
- Sorting is important
- Need to create records inside Salesforce sObjects
- Need to store the records from the SOQL Queries
- You need to store multiple dimensions like Matrix
Hands-On
// List variabname = new List();
List numbers = new List{4,5,6,6,6,6};
System.debug(' numbers '+ numbers);
List frtuits = new List(); // heap memory
frtuits.add('Grapes'); //0
frtuits.add('Mango'); //1
frtuits.add('Apple');//2
frtuits.add('Grapes');//3
frtuits.add('Banana');//4
// Size - 5
// Start Index = 0, Last Index = size - 1 = 4
System.debug(' frtuits '+ frtuits);
frtuits.set(1, 'Guava');
System.debug(' frtuits after set method '+ frtuits);
System.debug(' Index 2 '+ frtuits.get(5) );
String abc = frtuits.get(2);
Integer xyz = numbers.get(2); // String
//Illegal assignment from String (Right side after =) to Integer(left side before =)
Integer length = frtuits.size();
Boolean isEmpty = frtuits.isEmpty();
System.debug( length );
// 6MB - Sync , 12MB - Async
frtuits.clear();
System.debug( frtuits );
System.debug( frtuits.get(2) );
if(frtuits.isEmpty() == false){
System.debug(' List is Not Blank ');
}else{
System.debug(' List is Blank ');
}
Set in Salesforce
→ Set is an unordered set of collections elements.
→ Set does not contain the duplicate element
→ You can not access elements using indices
→ Below is visual representations of a set of String
Syntax of Set
Set colorsSet = new Set();
Set colorsSet = new Set{'Red', 'Black', 'Blue', 'Green'};
Set colorsSet;
Methods of Set in Salesforce
There are numerous methods of Set in Salesforce, however, there are some that are being used widely by developers.
- add(element)
- contains(element)
- addAll(another set or list)
- clear()
- remove(value)
- size()
- isEmpty()
- Ect
For more information refer to this link – https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_set.htm
When to Use Set
- Ordering is Not Important
- To Store the recordIds or Unique Elements like Emails, Phone
Hands-On
Set colorsSet = new Set{'black','Red', 'Black', 'Blue', 'Green','Black'};
System.debug(colorsSet);
System.debug(colorsSet.contains('Black') ) ;
Integer length = colorsSet.size();
Boolean isEmpty = colorsSet.isEmpty();
//colorsSet.clear();
Set fruitsSet = new Set();
fruitsSet.add('Apple');
fruitsSet.add('Banana');
fruitsSet.add('Mango');
colorsSet.addAll( fruitsSet );
System.debug(colorsSet);
Set numberSet = new Set{3,4,5,5,5};
colorsSet.addAll( numberSet );
Hands-On
Set colorsSet = new Set{'black','Red', 'Black', 'Blue', 'Green','Black'};
System.debug(colorsSet + '\n ');
System.debug(colorsSet.contains('Black') ) ;
Integer length = colorsSet.size();
Boolean isEmpty = colorsSet.isEmpty();
colorsSet.clear();
Set fruitsSet = new Set();
fruitsSet.add('Apple');
Account accountRecord = new Account();
accountRecord.Name = 'SET ACCOUNT DEMO';
Account accountRecord1 = new Account();
accountRecord1.Name = 'SET ACCOUNT DEMO';
Account accountRecord2 = new Account();
accountRecord2.Name = 'SET ACCOUNT DEMO2';
Set accountSet = new Set();
accountSet.add(accountRecord);
accountSet.add(accountRecord1);
accountSet.add(accountRecord2);
System.debug(accountSet);
List accountList = new List();
// accountList.add();
accountList.addAll(accountSet); // Set // Account
System.debug(accountList);
insert accountList;
/*
global class List{
global void addAll(List
Assignment
- Create a List to Store Some of the Salesforce Certifications
- Playaround with the methods
- Create a Set to store the Salesforce Certifications related to Salesforce Developers
- Playaround with the methods
https://trailhead.salesforce.com/en/credentials/administratoroverview/