Blog

Salesforce Collection of List

  • (4.0)
  • | 46 Ratings |
  • Last Updated February 04, 2019

Collection: When we want to save multiple data of “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.

Same type = all the data are integers or all are strings.
Common business rule = For eg age of all employees or salary of all employees.

Interested in mastering Salesforce? Enroll now for FREE demo on Salesforce Training

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

  • List.
  • Set.
  • Map.

List:

List has 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 : 

  • 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 <string> MyList = New List<string> ();
List <integer> MyList = New List<integer> ();</integer></integer></string></string>

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

Checkout SalesForce Tutorial

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

Code:

// declare a list variable
List <integer> EmpSalList = New List<integer> ();
//add values to the list
EmpSalList.add(10000);
EmpSalList.add(20000);
EmpSalList.add(30000);
EmpSalList.add(40000);
//values
system.debug(&#39;The Current Salary = &#39; + EmpSalList);</integer></integer>

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 <integer> EmpSalList = New List<integer> ();

//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(&#39;The value for 0 is  = &#39; + TempEmpSal);</integer></integer>

OutPut:

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

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

Code:

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

//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(&#39;The set value for 0 is =&#39; + EmpSalList);</integer></integer>

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 <integer> EmpSalList = New List<integer> ();

//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(&#39;The value for 0 is  = &#39; + TempEmpSal);

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

//To clear all values
EmpSalList.clear();
system.debug(&#39;The values =&#39; + EmpSalList);</integer></integer>

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 <integer> EmpSalList = New List<integer> ();

//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(&#39;The value for 0 is  = &#39; + TempEmpSal);

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

//To clear all values
EmpSalList.remove(0);
system.debug(&#39;The values =&#39; + EmpSalList);</integer></integer>

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 the particular value size?

Code:

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

//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(&#39;The value for 0 is  = &#39; + TempEmpSal);

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

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

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

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

About Author

Arogyalokesh

Arogyalokesh takes complex technical ideas and distills them into user-friendly visuals to improve digital marketing campaigns for companies. He is a Content Writer at Mindmajix spreading nuggets of content related to Technology. Proceeded to work in a fast upscale environment, assuring client delight in content writings


DMCA.com Protection Status