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 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.
If you have missed the previous Session, you can get the complete details from here
There are mainly three types of Loops available in Salesforce
for (Integer i=0; exit_condition; increment_stmt) {
code_block
}
// Fastest For Loop
for (Integer i = 0; list.size()-1; i++) {
code_block
}
for (sObject/String/Integer: list/set) {
code_block
}
for (sObject : [soql_query]) {
code_block
}
OR
for (list<variable> : [soql_query]) {
code_block
}
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
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++;
}

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