Questions tagged [switch-case]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
3 answers

How to avoid if-else/switch chains and preserve open/closed principle in Calculator program (apex) [Solution: Strategy Pattern]

TL;DR In apex, is it possible to get around the use of if-else/switch statements to supply unique code via an enum or other parameter? Something like storing functions or class instances in maps or ...
Jwok's user avatar
  • 767
1 vote
1 answer

problem with switch case

I share with you my code which appears to work well the problem I have is that: If for example, I choose the status as closed and I choose dates, I can't save and that's what I want, but when I try to ...
salesforcedev's user avatar
0 votes
1 answer

how to reduce lines of code in LWC

in my LWC project have a function per button, and I want to reduce lines and functions using a switch for the buttons. all function buttons are equal and i try use a switch, but I don't know how to ...
DDCruz's user avatar
  • 21
2 votes
1 answer

Switch/Case not working as expected - null occurs as more than one when branch for this switch statement

I would except the following code to work but I get the following error: null occurs as more than one when branch for this switch statement Any thought why? BTW, if I add the value in the when as a ...
Randall Flagg's user avatar
1 vote
1 answer

Can I perform a switch statement in AMPscript?

new SFMC user here. I'm just wondering if there's an equivalent of a javascript (or GTL) switch statement within AMPscript or if I'm restricted to using IF ELSE statements. For example, If I take the ...
marly's user avatar
  • 13
14 votes
1 answer

Most efficient way to switch on SObjectType?

It is a bit odd we can't evaluate a switch statement directly on SObjectType, so I am trying to determine conclusively which workaround is the most efficient. Just to verify, I tried to compile this ...
Adrian Larson's user avatar
  • 150k
24 votes
1 answer

Switch: Enum value used in 'when expression' should be unqualified

Background I am getting this error message three times: Enum value used in 'when expression' should be unqualified In relation to this code: Schema.DisplayType fieldType = getType(fieldName); ...
Robs's user avatar
  • 9,330
0 votes
4 answers

How can I access variable in switch case outside of switch block?

I am trying to upsert a dynamic sObject list of records using Database.Upsert, I want to be able to use AllOrNone parameter however this returns an error saying that my toUpsert variable must be a ...
Deployment Failure's user avatar
7 votes
1 answer

Good approach to making switch on string values case insensitive?

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 ...
Keith C's user avatar
  • 136k
6 votes
2 answers

Using newly released feature on an apex class having older API version

As you all probably know switch has been introduced in Summer'18 (API v43,0) release. I have started using it in my apex class which is having API version 36,0 and somehow it works in my sandbox. I ...
Lafexlos's user avatar
  • 499
17 votes
2 answers

Best Practice discussion: Difference between SWITCH and IF/ELSE-IF/ELSE?

NOTE: This is not an opinion-based question. Developers often debate the best way to code ways to do the same thing. Please don't close this as opinion-based. It's a good reference for people choosing ...
DavidSchach's user avatar
  • 5,874
8 votes
1 answer

System.TriggerOperation enum

Any examples how to use new System.TriggerOperation enum in a trigger? How this code could be replaced: trigger TriggerName on ObjectName (before delete, before insert, before update, after delete,...
Vacheslav Aronov's user avatar