In Apex the == operator is case-insensitive for strings and in other places case insensitivity is supported.

But the recently introduced switch is case sensitive. The workaround of moving to one case compiles for the value:

switch on operation.toLowerCase() {

but not for the comparison constants:

when 'EventActions.fire'.toLowerCase() {

Any good patterns for coding a case insensitive string switch in Apex? Or is it back to if/else if/else?

  • 1
    you know, I noticed this just the other day and went 'aargh'
    – cropredy
    Commented Sep 4, 2018 at 4:13

1 Answer 1


Switch statements, while welcome, are definitely still limited in functionality. It can't replace all possible situations you'd like to replace if-else statements with switch statements. Depending on your specific situation, you might be able to come up with a workaround. In your specific example, you'd write 'eventactions.fire', of course, but if you were using some other source, then it wouldn't make sense. While it's not necessarily ideal, I'd recommend trying to use switch over not, simply because it is easier. Also, switch will likely be expanded in future releases, so what's not possible today might be in the future.

  • Just disappointing behaviour given == is case insensitive. I'm using if/else if/else as the case insensitivity make the code cleaner.
    – Keith C
    Commented Sep 4, 2018 at 16:54
  • @KeithC true, but keep in mind that most languages have this problem. I think the exceptions are mostly scripting languages like Perl and Ruby. I'm just glad we have it at all.
    – sfdcfox
    Commented Sep 4, 2018 at 16:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .