Blog

Controlling the program flow in Salesforce

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

When we are having hundreds of lines of code to be executed, we need a mechanism by which you can control the execution. This is called as “Controlling Statements” in Salesforce.

In some cases, we require only few lines of code to be executed. In such scenarios, controlling statements take control in the execution based on the logic coded.

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

There are several ways to control the flow of the code to be executed, i.e.. through conditional statements, by conditional loops, etc.

We have different conditional statements. They are:

  • if , 
  • if else, 
  • else if. 

Syntax for if else:

if(condition){
 //code to be executed when condition is true
}
Else
{
 //code to be executed when condition is false
}

Syntax for else if:

if(condition){
 //code to be executed when condition is true
}
Else if(condition)
{
 //code to be executed when condition is false
}

When to use semicolon in the programming?

Except for class name, function name, conditional statement, we use “ ; ” at the end of the statement.
Example program for conditional statement execution:

Scenario - Voting Eligibility: The requirement for voting eligibility is that, the age of the person should be greater than or equal to 18. If it is greater than or equal to 18, we need to inform the person that he/she can vote, or else inform that he/she cannot vote.

Let us code for the above scenario.

Code:

integer age;
// assume this age is entered by end user via visualforce
age=15;
// Business logic
if(age >= 18)
{
System.debug (‘Congrats! You can vote’);
}
Else if(age<18)
{
System.debug (‘Sorry! You cannot vote’);
}

Output:

Sorry! You cannot vote

Scenario - Grading of student:

The requirement is like this, 

  • If the student gets marks below 40 - grade is “fail”.
  • If the student gets marks between 40 to 74 - grade “B”.
  • If the student gets marks more than 74+ - grade “A”.

Syntax for else if:

if(condition && condition){
 //code to be executed when condition is true
}
Else if(condition && condition)
{
 //code to be executed when condition is false
}
Else
{
//message other than the infinity conditions.
}

Let us code for the above scenario.

Code:

integer marks;
// assume this age is entered by end user via visualforce
marks=15;
// Business logic
if(marks > 0 && marks < 40)
{
System.debug (&#39;Fail&#39;);
}
Else if(marks >= 40 && marks <= 74)
{
System.debug (&#39;Grade - B&#39;);
}
Else if( marks < 74 && marks <= 100)
{
System.debug (&#39;Grade - A&#39;);
}
Else
{
System.debug (&#39;Marks seems to be not between 0 to 100&#39;);
}

Output:

Fail.

Checkout SalesForce Tutorial

Loop Controlling Statements:

It helps the statement to run the code multiple times. When there is a requirement to execute again and again, we can choose to set the code inside a loop controlling statement, rather than writing the code again. There are 2 types of loop control statements, they are For & While loops. They are explained below with syntaxes and examples.

Syntax for While Loop:

While(condition){
system.debug(&#39;Message to be displayed&#39;);
}

Scenario - Print “ I love my country ”

Code:

Integer counter;
Counter = 1;

// Business logic

while(counter <= 10)
{
system.debug(&#39;I love my country&#39;);
Counter = Counter + 1;
}

Or

Integer counter;
Counter = 10;

// Business logic

while(counter > 10)
{
system.debug(&#39;I love my country&#39;);
Counter = Counter - 1;
}

Syntax for For Loop:

For( initialise variable; condition; counter step)
{
// Business logic
}

Code for the example for printing “I love my country”

Integer counter;
for( counter=1; counter<=10; counter= counter +1 )
{
system.debug(‘I love my country’);
}

For loop Vs While loop Code:

While loops, like the For Loop, are used for repeating sections of code - but unlike a for loop, the while loop will not run ‘n’ times, but until a defined condition is no longer met. If the condition is initially false, the loop body will not be executed at all.

Checkout Salesforce Interview Questions

Code for the example for printing “I love my country” using While & For Loops:

While loop:

Integer counter;
Counter = 1;
while( counter <= 10)
{
system.debug(‘I love my country’);
}

For loop:

Integer counter;
for( counter=1; counter<=10; counter= counter +1 )
{
system.debug(‘I love my country’);
}

Sample program (scenario) using “For Loop”: 

Scenario: Consider initial value of Counter = 50, and using For loop, print the statement (I love my country) 20 times.

Code:

Program:

Integer counter;
for( counter=50; counter<=70; counter= counter +1 )
{
system.debug(&#39;I love my country! : &#39; + counter);
}

OutPut:

I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country
I love my country

Syntax: For Each Loop

For( temp variable declaration : collection )
{ 
// Business logic 
}

In the next topic, we will discuss in detail about “Salesforce Collection - List”. 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