Conditional Statements

Conditional statements in Salesforce Apex allow developers to control the flow of execution in their code based on specific conditions. These statements evaluate Boolean expressions and execute blocks of code depending on whether the expressions are true or false. The primary conditional statements in Apex are IF, ELSE IF, ELSE, and SWITCH.

1. IF Statement

The IF statement evaluates a Boolean condition and executes a block of code if the condition is true. If the condition is false, the block of code is skipped.

Syntax:

if (condition) {
    // Code to execute if condition is true
}

Example:

public class AccountHandler {
    public void updateAccountRating(Account acc) {
        if (acc.AnnualRevenue > 1000000) {
            acc.Rating = 'Hot';
        }
    }
}

Explanation:

  • In this example, the updateAccountRating method checks if the AnnualRevenue of an Account object is greater than 1,000,000. If it is, the Rating field of the account is set to ‘Hot’.

2. IF-ELSE Statement

The IF-ELSE statement allows for an alternative block of code to be executed if the IF condition is false.

Syntax:

if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

Example:

public class AccountHandler {
    public void updateAccountRating(Account acc) {
        if (acc.AnnualRevenue > 1000000) {
            acc.Rating = 'Hot';
        } else {
            acc.Rating = 'Warm';
        }
    }
}

Explanation:

  • Here, if the AnnualRevenue is greater than 1,000,000, the Rating is set to ‘Hot’. If it is not, the Rating is set to ‘Warm’. This ensures that the account always has a rating, even if it doesn’t meet the ‘Hot’ criteria.

3. IF-ELSE IF-ELSE Statement

The IF-ELSE IF-ELSE statement allows for multiple conditions to be checked in sequence. Once a true condition is found, its corresponding block of code is executed, and the remaining conditions are skipped.

Syntax:

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else {
    // Code to execute if both conditions are false
}

Example:

public class AccountHandler {
    public void updateAccountRating(Account acc) {
        if (acc.AnnualRevenue > 1000000) {
            acc.Rating = 'Hot';
        } else if (acc.AnnualRevenue > 500000) {
            acc.Rating = 'Warm';
        } else {
            acc.Rating = 'Cold';
        }
    }
}

Explanation:

  • In this example, if AnnualRevenue is greater than 1,000,000, the account is rated as ‘Hot’. If it’s not but is greater than 500,000, the account is rated as ‘Warm’. If neither condition is met, the account is rated as ‘Cold’. This provides a more granular classification based on revenue.

4. SWITCH Statement

The SWITCH statement evaluates a value against multiple possible matches (called cases) and executes the block of code associated with the first match. If no match is found, an optional DEFAULT case can be executed.

Syntax:

switch on (variable) {
    when value1 {
        // Code to execute if variable matches value1
    }
    when value2 {
        // Code to execute if variable matches value2
    }
    when else {
        // Code to execute if no previous cases match
    }
}

Example:

public class AccountHandler {
    public void setAccountCategory(Account acc) {
        switch on acc.Industry {
            when 'Technology' {
                acc.Category__c = 'Tech';
            }
            when 'Finance' {
                acc.Category__c = 'Financial';
            }
            when 'Healthcare' {
                acc.Category__c = 'Health';
            }
            when else {
                acc.Category__c = 'Other';
            }
        }
    }
}

Memory updated

Conditional Statements in Salesforce Apex

Conditional statements in Salesforce Apex allow developers to control the flow of execution in their code based on specific conditions. These statements evaluate Boolean expressions and execute blocks of code depending on whether the expressions are true or false. The primary conditional statements in Apex are IF, ELSE IF, ELSE, and SWITCH.

1. IF Statement

The IF statement evaluates a Boolean condition and executes a block of code if the condition is true. If the condition is false, the block of code is skipped.

Syntax:

apexCopy codeif (condition) {
    // Code to execute if condition is true
}

Example:

apexCopy codepublic class AccountHandler {
    public void updateAccountRating(Account acc) {
        if (acc.AnnualRevenue > 1000000) {
            acc.Rating = 'Hot';
        }
    }
}

Explanation:

  • In this example, the updateAccountRating method checks if the AnnualRevenue of an Account object is greater than 1,000,000. If it is, the Rating field of the account is set to ‘Hot’.

2. IF-ELSE Statement

The IF-ELSE statement allows for an alternative block of code to be executed if the IF condition is false.

Syntax:

apexCopy codeif (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

Example:

apexCopy codepublic class AccountHandler {
    public void updateAccountRating(Account acc) {
        if (acc.AnnualRevenue > 1000000) {
            acc.Rating = 'Hot';
        } else {
            acc.Rating = 'Warm';
        }
    }
}

Explanation:

  • Here, if the AnnualRevenue is greater than 1,000,000, the Rating is set to ‘Hot’. If it is not, the Rating is set to ‘Warm’. This ensures that the account always has a rating, even if it doesn’t meet the ‘Hot’ criteria.

3. IF-ELSE IF-ELSE Statement

The IF-ELSE IF-ELSE statement allows for multiple conditions to be checked in sequence. Once a true condition is found, its corresponding block of code is executed, and the remaining conditions are skipped.

Syntax:

apexCopy codeif (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else {
    // Code to execute if both conditions are false
}

Example:

apexCopy codepublic class AccountHandler {
    public void updateAccountRating(Account acc) {
        if (acc.AnnualRevenue > 1000000) {
            acc.Rating = 'Hot';
        } else if (acc.AnnualRevenue > 500000) {
            acc.Rating = 'Warm';
        } else {
            acc.Rating = 'Cold';
        }
    }
}

Explanation:

  • In this example, if AnnualRevenue is greater than 1,000,000, the account is rated as ‘Hot’. If it’s not but is greater than 500,000, the account is rated as ‘Warm’. If neither condition is met, the account is rated as ‘Cold’. This provides a more granular classification based on revenue.

4. SWITCH Statement

The SWITCH statement evaluates a value against multiple possible matches (called cases) and executes the block of code associated with the first match. If no match is found, an optional DEFAULT case can be executed.

Syntax:

apexCopy codeswitch on (variable) {
    when value1 {
        // Code to execute if variable matches value1
    }
    when value2 {
        // Code to execute if variable matches value2
    }
    when else {
        // Code to execute if no previous cases match
    }
}

Example:

apexCopy codepublic class AccountHandler {
    public void setAccountCategory(Account acc) {
        switch on acc.Industry {
            when 'Technology' {
                acc.Category__c = 'Tech';
            }
            when 'Finance' {
                acc.Category__c = 'Financial';
            }
            when 'Healthcare' {
                acc.Category__c = 'Health';
            }
            when else {
                acc.Category__c = 'Other';
            }
        }
    }
}

Explanation:

  • In this example, the setAccountCategory method uses the Industry field of the Account object to determine its category. If the industry is ‘Technology’, the category is set to ‘Tech’. For ‘Finance’, it’s ‘Financial’, and for ‘Healthcare’, it’s ‘Health’. If the industry doesn’t match any of these, the category is set to ‘Other’. The SWITCH statement is useful when dealing with a single variable that can have multiple discrete values.

Conclusion

Conditional statements like IF, ELSE IF, ELSE, and SWITCH are essential tools in Salesforce Apex for controlling the flow of logic in your code. They allow developers to create dynamic applications that respond to varying conditions and inputs, making them highly customizable and efficient. Understanding and effectively utilizing these statements is crucial for any Apex developer.


Tutorials Deck

TutorialsDeck is striving to provide the best learning material on technical and non-technical subjects.

Languages

Web Technologies

Database

Trending Technologies

© 2024. All rights reserved.

Contact Us @ tutorialsdeck06@gmail.com