Blog

Salesforce Collection of List

  • (5.0)
  • | 1205 Ratings
Salesforce Collection of List

Collection

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 eg age of all employees or the salary of all employees.

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

  • List.
  • Set.
  • Map.

List

The list has a collection of ordered elements. It means, each and every element’s place is fixed or we can tell where an element will be, by using index number.

Collection - List

  • The list is an ordered collection of elements that are distinguished by indices.
  • The index position of the first element in the list is always zero.
  • It can store primitive data types (integer, string), SObjects.
  • It can have duplicate values.

Syntax;

List  MyList = New List ();
List  MyList = New List ();

Sample program for List

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 the salary of the employees via List.

Checkout SalesForce Tutorial

Program#1 - How to add values to the list?

Code:

// declare a list variable
List  EmpSalList = New List ();
//add values to the list
EmpSalList.add(10000);
EmpSalList.add(20000);
EmpSalList.add(30000);
EmpSalList.add(40000);
//values
system.debug('The Current Salary = ' + EmpSalList);

OutPut:

17:21:11:002 USER_DEBUG [12]|DEBUG|The Current Salary = (10000, 20000, 30000, 40000

Program#2 - How to get values from positions?

Code:

// declare a list variable
List  EmpSalList = New List ();

//add values to the list
EmpSalList.add(10000);
EmpSalList.add(20000);
EmpSalList.add(30000);
EmpSalList.add(40000);

//get values from position
Integer TempEmpSal = EmpSalList.get(0);
system.debug('The value for 0 is  = ' + TempEmpSal);

OutPut:

17:21:11:002 USER_DEBUG [12]|DEBUG|The set value for 0 is =10000

Program#3 - How to set a value at a certain position?

Code:

// declare a list variable
List  EmpSalList = New List ();

//add values to the list
EmpSalList.add(10000);
EmpSalList.add(20000);
EmpSalList.add(30000);
EmpSalList.add(40000);

//set value for certain position
EmpSalList.set(0, 50000);
system.debug('The set value for 0 is =' + EmpSalList);

OutPut:

17:58:05:002 USER_DEBUG [15]|DEBUG|The set value for 0 is =(50000, 20000, 30000, 40000)

Checkout Salesforce Interview Questions

Program#4 - How to clear all the values?

Code:

// declare a list variable
List  EmpSalList = New List ();

//add values to the list
EmpSalList.add(10000);
EmpSalList.add(20000);
EmpSalList.add(30000);
EmpSalList.add(40000);

//get values from position
Integer TempEmpSal = EmpSalList.get(0);
system.debug('The value for 0 is  = ' + TempEmpSal);

//set value for certain position
EmpSalList.set(0, 50000);
system.debug('The set value for 0 is =' + EmpSalList);

//To clear all values
EmpSalList.clear();
system.debug('The values =' + EmpSalList);

OutPut:

18:03:12:002 USER_DEBUG [12]|DEBUG|The value for 0 is  = 10000
18:03:12:002 USER_DEBUG [16]|DEBUG|The set value for 0 is =(50000, 20000, 30000, 40000)
18:03:12:002 USER_DEBUG [20]|DEBUG|The values =()

Program#5 - How to remove one value instead of all values?

Code:

// declare a list variable
List  EmpSalList = New List ();

//add values to the list
EmpSalList.add(10000);
EmpSalList.add(20000);
EmpSalList.add(30000);
EmpSalList.add(40000);

//get values from position
Integer TempEmpSal = EmpSalList.get(0);
system.debug('The value for 0 is  = ' + TempEmpSal);

//set value for certain position
EmpSalList.set(0, 50000);
system.debug('The set value for 0 is =' + EmpSalList);

//To clear all values
EmpSalList.remove(0);
system.debug('The values =' + EmpSalList);

OutPut:

18:09:51:022 USER_DEBUG [12]|DEBUG|The value for 0 is  = 10000
18:09:51:022 USER_DEBUG [16]|DEBUG|The set value for 0 is =(50000, 20000, 30000, 40000)
18:09:51:022 USER_DEBUG [20]|DEBUG|The values =(20000, 30000, 40000)

Program#6 - How to get a particular value size?

Code:

// declare a list variable
List  EmpSalList = New List ();

//add values to the list
EmpSalList.add(10000);
EmpSalList.add(20000);
EmpSalList.add(30000);
EmpSalList.add(40000);

//get values from position
Integer TempEmpSal = EmpSalList.get(0);
system.debug('The value for 0 is  = ' + TempEmpSal);

//set value for certain position
EmpSalList.set(0, 50000);
system.debug('The set value for 0 is =' + EmpSalList);

//To clear all values
EmpSalList.remove(0);
system.debug('The values =' + EmpSalList);

//length of the value
Integer length = EmpSalList.size(1);
system.debug(‘The Size of 1 is ’ + length);

OutPut:

18:25:32:002 USER_DEBUG [12]|DEBUG|The value for 0 is  = 10000
18:25:32:002 USER_DEBUG [16]|DEBUG|The set value for 0 is =(50000, 20000, 30000, 40000)
18:25:32:002 USER_DEBUG [24]|DEBUG|The Size of 1 is 3.

Assignment: Create a list of 10 employees, Salaries. Print their salaries one by one 10 times using system.debug().

Hint: List  + for each loop.

In the next topic, we will discuss in detail about “Salesforce Collection - Set”. 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

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The 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.


DMCA.com Protection Status

Close
Close