Switch activity with string variable in expression

Hello all,

In any language i’ve programmed a Case statement is a shortcut for when an If statement has many different possible output values. I programmed a Case in UIPath like below, and it is not working which i believe cause it thinks “VSecurityAnswer” is constant text, but it’s really a variable. Some answers in the forum here said you can only have 2 case responses which defeats the purpose, and some had people put an If statement of code (not activity) in the Expression line, which defeats the purpose. I’m about to just do 3 If statement activities, but i just want to confirm that Case statements are useless for this in UIPath? Or is there a way to tell UIPath that the string in the Expression box is an actual variable and not text. I’m really surprised you can;t just type a variable, and for constant text you would just have it in double quotes.

[EDIT] - I found the below video made by UIPath devs and they are using the Switch exactly as i would like to. The only difference is that i am doing string compares, and the Cases are variables. So this should be possible. Switch Case UiPath||Switch Activity||UiPath RPA Tutorial - YouTube

[EDIT 2] - This below was what i was trying to avoid but i did 3 nested IFs. Pretty ugly but it works :frowning: . If someone knows how to accomplish a string comparison in a cleaner Switch, I’d still like to see it.

1 Like

Hi @jmalone

I went through the question of yours. From what I know, the switch activity works kind of in a different way.

For the switch activity, once you specify the variable in the expression area, it always look for value types that is only available in that variable which you have provided. So basically, in your case, the expression of switch activity is vSecurityQuestion. So, it will need to know what values it holds as cases to check. Once you expand a case, there you get a place to type the value of the case. Here you have typed “in_config…” which actually gets the value from a config dictionary. However, this is not possible here :slight_smile:

So, once you provide the expression for switch and set it as string, whatever the value you provide in cases will be considered as the value it needs to look for. So in your case, you have vSecurityQuestion as a string expression. So even though you specify in_config… expression in your case part, it will not look for values in the in_config, but instead, it will consider “In_Config…” as your value. That’s why your case is failing to execute the cases.

For each case, you need to specify the exact value you need to check, not the expression of where it should get the value from… I think that’s the normal function of a switch case in programming because values for a switch cannot be dynamic as I know…

That’s why in the video you attached, they have the exact value in the switch case.
image

However, the IF activity works in a different way as it can handle such stuff. So if case is not really going to work for you, I would suggest to go ahead with a FlowChart activity which gives you greater clarity over the conditions and personally, I don’t agree on having nested IF’s in the workflow as a good practice…

Hope this helps…

1 Like

Like @Lahiru.Fernando was saying (I think), you can’t use a variable as the Case value. To be honest, I don’t know what the reasoning for that is.

However, there are some things you can consider as a solution…

If you use “NSSecQ1”, “NSSecQ2”, and “NSSecQ3” as your Case strings instead of using in_Config, then it becomes simpler.

You can store those in an array { "NSSecQ1", "NSSecQ2", and "NSSecQ3" }
Then, use it in the expression to find the string that matches vSecurityQuestion

For example,

Assign matchedQ = { "NSSecQ1", "NSSecQ2", and "NSSecQ3" }.Where(Function(q) vSecurityQuestion.ToUpper.Trim = in_Config(q).ToString.ToUpper.Trim ).ToArray

Switch
Expression: If(matchedQ.Count > 0, matchedQ(0), "")
Default
Case: NSSecQ1
Case: NSSecQ2
Case: NSSecQ3

But, I agree, it is annoying that you can’t use a variable in the Case

Regards.

2 Likes