In Salesforce, these keywords are used primarily in Apex, which is the proprietary programming language used to create and customize business logic in Salesforce applications. Apex is similar to Java and includes familiar control structures, such as if
, else
, do
, while
, for
, break
, and continue
. Here’s a brief explanation of how these keywords work in Apex
if, else, do, while, for, break, continue
if Statement
The if keyword defines a conditional statement, used to determine whether a code block should be executed based on a condition.
Example
Integer score = 85;
if (score >= 60) {
System.debug('Pass');
}
If score
is greater than or equal to 60, the message “Pass” will be printed in the debug log.
else Statement
The Else Statement can be paired with if to specify an alternative block of code that runs when the condition is False. Defines the else portion of an if-else statement, that executes if the initial evaluation is untrue.
Example
Integer score = 45;
if (score >= 60) {
System.debug('Pass');
} else {
System.debug('Fail');
}
If score
is not greater than or equal to 60, then “Fail” is printed in the debug log.
while
while keyword executes a block of code repeatedly as long as a particular boolean condition remains true.
Example
Integer counter = 1;
while (counter <= 5) {
System.debug('Counter: ' + counter);
counter++;
}
This loop runs while counter
is less than or equal to 5, printing the counter each time and then incrementing it.
do
do keyword defines a do-while loop that executes repeatedly as long as a boolean condition evaluates to true.
Example
Integer count = 1; do { System.debug(count); count++; }
for
for keyword is used to define a loop. There are three types of for loops.
- iteration using a variable
- iteration over a list
- iteration over a query.
Example
// loop that iterates based on a condition for (Integer i = 0, j = 0; i < 10; i++) { System.debug(i+1); } Integer[] myInts = new Integer[]{1,8, 9}; // loop that iterates for each element of an array for (Integer i : myInts) { System.debug(i); } String s = 'Acme'; // loop that iterates for each row in the resultset for (Account a : [SELECT Id, Name,FROM account WHERE Name LIKE :(s+'%')]) { // Your code }
Break Statement
Break keyword is used to exit the enclosing loop prematurely.
In the following example, break keyword is used to break the while loop prematurely when a specific condition is met.
while(reader.hasNext()) { if (reader.getEventType() == END) { break; }; // process reader.next(); }
Continue Statement
Continue keyword skips to the next iteration of the loop. The statements in the loop block after the continue statement are skipped and the execution continues with the loop again.
Example
for (Integer i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.debug('Iteration: ' + i);
}
When i
is equal to 5, the continue
statement will skip printing that iteration and move to the next one. The loop will print numbers from 0 to 9, except for 5
These control structures are useful for implementing business logic and iterating over records, particularly when dealing with Salesforce data. For example:
- If/else conditions are used to execute specific logic based on field values of Salesforce objects.
- Loops (
for
,while
) are often used to iterate through lists of records to perform mass updates, validations, or calculations. break
andcontinue
can be helpful in situations like looping over records until a specific criterion is met.
How these are used in Salesforce?
List<Lead> leads = [SELECT Id, Status FROM Lead WHERE Status = 'New'];
for (Lead lead : leads) {
if (lead.Status == 'New') {
System.debug('Processing new lead with ID: ' + lead.Id);
} else {
continue; // Skip any leads that aren't new.
}
}
The above code selects all new leads and processes only those leads with a status of New
. The continue
keyword ensures that any non-New
leads are skipped.
Conclusion
These control structures (if
, else
, do
, while
, for
, break
, and continue
) are very powerful and flexible in Apex, just like in any other programming language. They help control the flow of execution, which is critical when handling business requirements, processing data, or automating workflows in Salesforce.