A switch statement in programming is a type of control mechanism that lets you test a variable against multiple values. Each of these values is referred to as a “case”, and the switch statement evaluates the variable to see if it matches any of these cases.
In this blog, you’ll gain insights into:
- Understanding the fundamental concept and syntax and usages
- Explore practical examples that demonstrate the apex switch statement’s versatility in real-world scenarios.
- Discover the unique characteristics of Apex switch statements, including handling multiple forms and null values.
Let’s get started!
Understanding the Syntax and Usage
The switch statement in Apex tests an expression against a set of values and executes corresponding code blocks. The syntax is straightforward:
switch on expression {
when value1 {
// code for value1
}
when value2 {
// code for value2
}
…
when else {
// default code block
}
}
Key Characteristics:
- Multiple Forms: The when value can be a single value, multiple values, or sObject types.
- No Fall-through: Unlike some languages, Apex’s switch does not fall through to subsequent cases after a match.
- Types Supported: The expression can be of types like Integer, Long, sObject, String, or Enum.
- Unique When Values: Each when value in a switch must be unique.
- Handling Null Values: Apex being nullable, a when value can be null.
Also Read – Apex Best Practices in Salesforce
The When Else Block:
It’s recommended, especially with enum types, to include a when else block. This is to handle unexpected values, such as those introduced in new versions of a managed package.
Practical Examples
Examples with Literals:
For Integer, Long, and String types, you can use literal when values. Note that string clauses are case-sensitive.
Single Value Example:
switch on i {
when 2 {
System.debug(‘when block 2’);
}
…
when else {
System.debug(‘default’);
}
}
Null Value Example:
switch on i {
when null {
System.debug(‘bad integer’);
}
…
}
Multiple Values Examples:
A single when clause can include multiple literal values.
apex
switch on i {
when 2, 3, 4 {
System.debug(‘when block for 2, 3 and 4’);
}
…
}
Method Example:
Switching on the result of a method call is also possible.
switch on someInteger(i) {
when 2 {
System.debug(‘when block 2’);
}
…
}
Example with sObjects:
This allows for instanceof checks and casting implicitly.
switch on sobject {
when Account a {
System.debug(‘account ‘ + a);
}
…
}
Example with Enums:
Utilizing enums in switch statements is efficient and readable.
switch on season {
when WINTER {
System.debug(‘boots’);
}
…
}
Conclusion
Apex’s switch statement offers a clean, readable, and efficient way to control program flow based on varying conditions. By understanding and utilizing this feature, Salesforce developers can write more maintainable and efficient Apex code, enhancing the functionality and user experience of Salesforce applications.
Remember, always plan for unexpected cases, particularly when dealing with external packages or dynamic data—the when else block is not just a fallback but a crucial part of robust Apex programming.
If you’re looking to take your Salesforce skills to the next level, don’t miss out on our Salesforce developer bootcamp at saasguru. Our hands-on training approach, centered around real projects, is designed to give you a practical learning experience that goes beyond theory.
By participating in this bootcamp, you’re not just learning; you’re preparing to excel in the Salesforce world.