Experience Salesforce
Logic Control and Looping Statements in Salesforce
What You’ll Learn
- Logic Control And Looping Statement
- Types Of Logic Control
- Looping Statement
A critical aspect of Apex programming involves using logic control and looping statements. These constructs enable developers to create efficient, dynamic, and scalable code. In this chapter we will delve into the types of logic control and looping statements in Salesforce Apex, providing examples and practical insights.
Logic Control and Looping Statements
Logic control and looping statements are fundamental programming constructs that allow developers to control the flow of their code based on certain conditions and to repeat a block of code multiple times. These tools are essential for handling complex logic, iterating over collections, and performing repetitive tasks efficiently.
Types of Logic Control
Logic control in Salesforce Apex includes conditional statements that help manage the flow of execution based on specific conditions. The primary types of logic control are:
If Statement
The if statement evaluates a boolean condition and executes the block of code within it if the condition is true.
if (condition) { // code to be executed if condition is true} |
Example
Integer score = 85;if (score > 75) { System.debug(‘You passed the exam!’);} |
If-Else Statement
The if-else statement provides an alternative block of code to execute if the condition is false.
if (condition) { // code to be executed if condition is true} else { // code to be executed if condition is false} |
Example
Integer score = 65;if (score > 75) { System.debug(‘You passed the exam!’);} else { System.debug(‘You need to retake the exam.’);} |
Else-If Ladder
This allows multiple conditions to be checked in sequence.
if (condition1) { // code to be executed if condition1 is true} else if (condition2) { // code to be executed if condition2 is true} else if (condition3) { // code to be executed if condition3 is true} else { // code to be executed if all conditions are false} |
Example
Integer score = 65;if (score > 90) { System.debug(‘Excellent!’);} else if (score > 75) { System.debug(‘Good Job!’);} else if (score > 60) { System.debug(‘You passed.’);} else { System.debug(‘You need to improve.’);} |
Switch Statement
The switch statement allows a variable to be tested against multiple values.
switch on variable { when value1 { // code to be executed if variable equals value1 } when value2 { // code to be executed if variable equals value2 } when else { // code to be executed if variable doesn’t match any value }} |
Example
String grade = ‘B’; switch on grade { when ‘A’ { System.debug(‘Excellent’); } when ‘B’ { System.debug(‘Good’); } when ‘C’ { System.debug(‘Average’); } when else { System.debug(‘Needs Improvement’); }} |
Types of Loops in Salesforce Apex
Loops are used to execute a block of code repeatedly. Salesforce Apex supports several types of loops.
For Loop
The for loop is used to execute a block of code a specific number of times. It consists of three parts: initialization, condition, and increment.
Syntax:
for (initialization; condition; increment) { // code to execute} |
Example:
for (Integer i = 0; i < 5; i++) { System.debug(‘Iteration: ‘ + i);} |
While Loop
The while loop continues to execute a block of code as long as the condition is true.
Syntax:
while (condition) { // code to execute} |
Example:
Integer i = 0;while (i < 5) { System.debug(‘Iteration: ‘ + i); i++;} |
Do-While Loop
The do-while loop is similar to the while loop, but it guarantees that the code block will execute at least once before the condition is checked.
Syntax:
do { // code to execute} while (condition); |
Example:
Integer i = 0;do { System.debug(‘Iteration: ‘ + i); i++;} while (i < 5); |
Loops in Salesforce Flow
Salesforce Flow provides a visual way to automate processes without writing code. In Flow, loops are used to iterate over collections of records or data.
Example of Loop in Salesforce Flow
- Create a Collection Variable: Store multiple records.
- Add a Loop Element: Iterate over the collection.
- Add Logic Inside the Loop: Perform actions on each record.
Use Case Example:
Imagine you need to update the status of multiple case records. In Salesforce Flow, you can create a loop to iterate over each case record, updating the status accordingly.
Next TopicNeed Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.