Salesforce Collection of Set

Salesforce Collection of Set 

When we want to save multiple data of “the same type” and also connect with each other with some “common business rule”, then instead of creating multiple variables, we need to create one variable of type collection.

The same type = all the data are integers or all are strings.
Common business rule = For engage of all employees or the salary of all employees.

Collections are the same as an array” - We have 3 types of collections in Salesforce, and they are:

  1. List.
  2. Set.
  3. Map.

Set

  • Sets are an unordered collection of elements.
  • They will not contain any duplicate values.
  • Set elements can be primitive types (string,integer), SObject - (Salesforce Objects - Account, Lead, Cases etc), user defined types (classes).

Lets us look into collection-set with a sample program

Scenario: Add salaries of employees in the list and process them with step by step execution with collection functions The below mentioned are 6 individual programs for adding salary of the employees via Set.

Checkout SalesForce Tutorial

Program#1 - How to create a “Set” to store Bank Acc Num of Employee?

Initial Code:

// declare a list variable
Set  EmpBankAcc = New Set ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);
EmpBankAcc.add(444);

//values
system.debug('The Current Account Number = ' + EmpBankAcc);

OutPut:

11:41:49:004 USER_DEBUG [11]|DEBUG|The Current Account Number = {111, 222, 333, 444}

Program#2 - How to find the size of the ACC Numbers created?

Initial Code:
// declare a list variable
Set  EmpBankAcc = New Set ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);
EmpBankAcc.add(444);

//values
system.debug('The Current Account Numbers = ' + EmpBankAcc);

//for size
Integer length=EmpBankAcc.size();
system.debug(' The Current size of the Bank Acc Numbers =  ' + length);

OutPut:

12:05:35:002 USER_DEBUG [11]|DEBUG|The Current Account Numbers = {111, 222, 333, 444}
12:05:35:003 USER_DEBUG [15]|DEBUG| The Current size of the Bank Acc Numbers =  4

 MindMajix YouTube Channel

Program#3 - what if a duplicate value is added in “Set” in the anonymous window?

Initial Code:
// declare a list variable
Set  EmpBankAcc = New Set ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug('The Current Account Numbers = ' + EmpBankAcc);

//duplicate value added
EmpBankAcc.add(111);

//duplicate  values print
system.debug('The Current Account Numbers = ' + EmpBankAcc);

OutPut:

12:21:38:002 USER_DEBUG [11]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:21:38:002 USER_DEBUG [15]|DEBUG|The Current Account Numbers = {111, 222, 333}

Note: It just ignores the duplicate values, added and displays the remaining values.

Checkout Salesforce Interview Questions

Program#4 - How to check whether the value (something assigned/add) is existing in the set or not?

Initial Code:
// declare a list variable
Set  EmpBankAcc = New Set ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug('The Current Account Numbers = ' + EmpBankAcc);

//check if set contains value
boolean CheckTheValue = EmpBankAcc.contains(444);

//print statement for checking the value existing or not?
System.debug ( 'Does this set contains 444? = ' + CheckTheValue);

OutPut:

12:30:50:002 USER_DEBUG [10]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:30:50:002 USER_DEBUG [14]|DEBUG|Does this set contains 444? = false

Program#5 - How to remove the value (something assigned/add) in the set?

Initial Code:
// declare a list variable
Set  EmpBankAcc = New Set ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug('The Current Account Numbers = ' + EmpBankAcc);

//Remove value from set
EmpBankAcc.remove(333);

//print statement for Current Set
system.debug('Current Set = ' + EmpBankAcc);

OutPut:

12:39:02:003 USER_DEBUG [10]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:39:02:003 USER_DEBUG [16]|DEBUG|Current Set = {111, 222}

Program#6 - How to check whether the value (something assigned/add) is empty in the set or not?

Initial Code:
// declare a list variable
Set  EmpBankAcc = New Set ();

//add values to the list
EmpBankAcc.add(111);
EmpBankAcc.add(222);
EmpBankAcc.add(333);

//values print
system.debug('The Current Account Numbers = ' + EmpBankAcc);

//check if set contains value
boolean EmptyCheck = EmpBankAcc.isempty();

//print statement for checking the value existing or not?
System.debug ( 'Is this set empty? = ' + EmptyCheck);

OutPut:

12:57:33:002 USER_DEBUG [10]|DEBUG|The Current Account Numbers = {111, 222, 333}
12:57:33:002 USER_DEBUG [16]|DEBUG|Is this set empty? = false

Assignment: Create a set of 10 email IDs. Print one by one using 10 times using system.debug().
Hint: SET + for each loop.

In the next topic, we will discuss in detail about “Salesforce Collection of Map”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training Salesforce Lightning Training 
Salesforce Advanced Developer Training Salesforce Developer Training 
Salesforce IoT Training Salesforce App Builder Certification Training 
Salesforce AppExchange Training Salesforce Service Cloud Training 
and many more. 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Salesforce TrainingDec 07 to Dec 22View Details
Salesforce TrainingDec 10 to Dec 25View Details
Salesforce TrainingDec 14 to Dec 29View Details
Salesforce TrainingDec 17 to Jan 01View Details
Last updated: 03 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles