Using Switch Statement to check if a string contains a word

I want to use a switch statement to check a string for the words “SUCCESS” and “FAILED” So far I have

SwitchStatement

But even though the message contains “SUCCESS” It still prints out the “UNKNOWN” Status.

The full Status message looks like this.
SUCCESS DEFAULT SUCCESS
Or FAILED DUPLICATE_ORDER Duplicate order recieved
Or if it’s any other message it should say unknown, if it doesn’t contain SUCCESS or FAILED

Am I doing something wrong here?

1 Like

I think you’re problem is that your expression always equals the full string so there is no Case for it to go to (I probably confused you).

Try using your .Contains in the expression, then use the String “SUCCESS” as the Case.

I’m not aware of a way to use a condition as the Case, so I normally just use the condition in the Expression, like I have presented in the example. I also added .ToUpper to remove case-sensitivity.

Regards.

3 Likes

When I typed that in, I get an error message in the Expression.
Compiler error(s) encountered processing expression
Cannot infer a common type because more than one type is possible.

So I don’t think the expression is working. But I see what you’re trying to do here, I just can’t think
of a way to express that…

Can you show what your Switch and Expression looks like? It’s probably something simple.

Oh sorry, there was a False missing from the If statement.

If(StatusMessage.ToUpper.Contains("SUCCESS"),"SUCCESS",If(StatusMessage.ToUpper.Contains("FAILED"),"FAILED", "Default") )

i added the False to “Default”, so you can try that.

3 Likes

Capture

Okay, that seemed to make the errors go away. Now I’ll have to test it out. Thanks

Hi @HsDev,

Another simple possibility may be to use decision activity instead of switch activity.

Best Regards,
Susana

2 Likes

So I didn’t get any errors, but it didn’t choose the correct case. The status contained FAILED, but it chose the Default case instead of FAILED.

When I printed out the status in the log, it definitly contains the word failed. Capture

I changed StatusMessage from Generic value to a String, and got rid of .ToString. in my expression, but it’s still giving me the default value instead of failed.

I’m trying to keep it short and visible, and I don’t want to have to add another workflow to my sequence. So I’m trying to use the switch statement to keep it all in site. Because I already have 5 workflows that are pretty long, I just want to shorten them up however I can.

I would say before your switch, paste your expression into a Message Box to verify what the output of the expression is returning. Also, verify that StatusMessage is in All Caps and contains the word correctly.

Hope that helps.

Sorry, server froze up. I’ll try now

It prints FAILED like it’s supposed to. I don’t know why it’s just automatically going to the default case in the switch statement…

Can you remove the “” from the case values and try
I tried and CASE - “Failed” should be without quotes just FAILED and type you may already have as String

4 Likes

Sweet. It worked. Thank you. So this is the final Switch statement I have. For future reference.

8 Likes