SaasGuru Logo

Flat 20% OFF on Salesforce Marketing Cloud and Data Cloud + AI Bootcamp 🎉– Offer Ends 30th Nov!

Flat 20% OFF on Salesforce Marketing Cloud and Data Cloud + AI Bootcamp 🎉– Offer Ends 30th Nov!
Switch Statements in Salesforce Apex

 Switch Statements in Salesforce Apex

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

    }

}

saasguru Salesforce Labs: Real-time Projects for Practice

Key Characteristics:

  1. Multiple Forms: The when value can be a single value, multiple values, or sObject types.
  2. No Fall-through: Unlike some languages, Apex’s switch does not fall through to subsequent cases after a match.
  3. Types Supported: The expression can be of types like Integer, Long, sObject, String, or Enum.
  4. Unique When Values: Each when value in a switch must be unique.
  5. 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.

Salesforce Training Program India

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.

Table of Contents

Subscribe & Get Closer to Your Salesforce Dream Career!

Get tips from accomplished Salesforce professionals delivered directly to your inbox.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Unlock Your AI -Powered Assistant

Gain Exclusive Access to Your Salesforce Copilot

Related Articles

Humans of Salesforce – Mauricio Alexandre Silva

Discover Mauricio Alexandre Silva’s inspiring Salesforce journey, tips for success, and insights into the evolving ecosystem. Read now!

Salesforce India Achieves $1 Billion Revenue Milestone

Salesforce India achieves $1 billion revenue milestone, driven by AI, innovation, and digital transformation across industries. Read now!

Most Demanded Courses in IIT Kanpur in 2025

Explore IIT Kanpur’s most demanded courses in 2025, including Salesforce, AI, Data Science, and more. Read now!