How to Work with Loops in Salesforce?

Loops are a very important and integral part of Salesforce Development! So in this blog post we will learn what are the different types of loops in Salesforce and how to use them.

Loops in Salesforce

Loops are a very important and integral part of Salesforce Development! So in this blog post we will learn what are the different types of loops in Salesforce and how to use them.

Previous Session

If you have missed the previous Session, you can get the complete details from here

Types of Loops

There are mainly three types of Loops available in Salesforce

  • For Loop
  • While Loop
  • Do While Loop

Types of For Loop in Salesforce

1. The Traditional For Loop

for (Integer i=0; exit_condition; increment_stmt) {
    code_block
}

// Fastest For Loop
for (Integer i = 0; list.size()-1; i++) {
    code_block
}

2. The list or set iteration for the loop

for (sObject/String/Integer: list/set) {
    code_block
}

3. The SOQL for loop

for (sObject : [soql_query]) {
    code_block
}

OR

for (list<variable> : [soql_query]) {
    code_block
}

Hands-On For Loop

List<String> fruits = new List<String>{'Apple', 'Banana', 'Mango'};
/*
    for (Integer i=0; exit_condition; increment_stmt) {
        code_block
    }
    i++ => i = i+1
*/
Integer length = fruits.size();
for(Integer i = 0; i < length; i++ ){
    String fruitName = fruits.get(i);
    System.debug(' '+  fruitName);
}
// Iteration #1 
// i = 0
// 0 < 3 = True
// String fruitName = fruits.get(0);
// Apple

// Iteration #2
// i = 1
// 1 < 3 = True
// String fruitName = fruits.get(1);
// Banana

// Iteration #3
// i = 2
// 2 < 3 = True
// String fruitName = fruits.get(2);
// Mango

// Iteration #4
// i = 3
// 3 < 3 = False

List<String> fruits = new List<String>{'Apple', 'Banana', 'Mango'};
/*
for (sObject/String/Integer : list/set) {
    code_block
}
*/
for(String fruitName : fruits){
    System.debug(' '+  fruitName);
}
Integer length = fruits.size();
for(Integer i = 0; i < length; i++ ){
    String fruitName = fruits.get(i);
    //System.debug(' '+  fruitName);
}

// No Index - No Get Method
// Email, Phone, RecordIds
Set<Integer> simpleSet = new Set<Integer>{2,5,6,7,8,8};
for(Integer n : simpleSet){
    System.debug(' '+  n);
}

Map<String, Integer> employeesMap = new Map<String, Integer>{
    'Acme' => 238, 'Acme Inc' => 24538, 'United Oil' => 25538
};

String key = 'United Oil';
System.debug( employeesMap.get(key) );

// keySet() ~= Set<String>
// values() ~= List<Integer>
/*
    Set<String> keySet = employeesMap.keySet();
*/
// Set<String> keySet = employeesMap.keySet(); // Heap Memory (Size)
// List/Set Iteration For Loop
Set<String> keySet = employeesMap.keySet();
// { 'Acme', 'Acme Inc', 'United Oil' }
for(String key : keySet ){
    System.debug(' Key is '+ key);
    System.debug(' value for '+key+' is '+ employeesMap.get(key) );
}

// Iteration #1
// key = Acme
// value = 238

// Iteration #2
// key = Acme Inc
// value = 24538

// Iteration #3
// key = United Oil
// value = 25538

While Loop

Syntax

while(condition){ // True/False
// code block statements
}
List<String> fruits = new List<String>{'Apple', 'Banana', 'Mango'};
Integer i = 0;
while(i < fruits.size() ){
    System.debug(' '+fruits.get(i) );
    i++;
}


Hands-On

List<String> fruits = new List<String>{'Apple', 'Banana', 'Mango'};
Integer i = 0;
while(i < fruits.size() ){
    System.debug(' '+fruits.get(i) );
    i++;
}

Watch Complete Video

Assignments

  1. Create a List to Store Some of the Salesforce Certifications
  2. Create a Set to store the Salesforce Certifications related to Salesforce Developers
  3. Create a Map to store the Salesforce Certifications in the following Fashion – Note you can get the list of all the certifications by Job Role from here – https://trailhead.salesforce.com/en/credentials/administratoroverview/
    1. Key – Developer – Value – List of All the Certifications
    2. Key – Admin – Value – List of All the Certifications
    3. Key – Architect – Value – List of All the Certifications
  4. Use the collections from the previous exercise and iterate those using For Loop and Do while Loop
  5. Print the Information using the Debug Log

Resources

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading