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:
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.
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}
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
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
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
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}
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.
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
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:
Name | Dates | |
---|---|---|
Salesforce Training | Nov 23 to Dec 08 | View Details |
Salesforce Training | Nov 26 to Dec 11 | View Details |
Salesforce Training | Nov 30 to Dec 15 | View Details |
Salesforce Training | Dec 03 to Dec 18 | View Details |
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.