How to Use a Switch Activity for Contains Text in UiPath Studio

You might be knowing that Switch statements are mainly used for Equals conditions. In this post, we will use the Switch Activity for contains text conditions.

Prerequisites

  1. UiPath Studio

Let us take an example of finding a word inside a sentence using switch activity. First, we assign the value to our message.

I am using an assign activity. Declaring a variable message and initializing with value “Test case passed“. Let us define 3 possible message types –

  1. Test case passed
  2. Test case failed
  3. Some error occurred

You can change the types based on your project use case. Let us write an if condition to fulfill this for the switch case. I have used nested if here.

If(message.ToLower.Contains("passed"),"Passed",If(message.ToLower.Contains("failed"),"Failed","Error"))

Below is the syntax for the above if statement.

IF(logical_test, value_if_true, [value_if_false])
Argument nameDescription
logical_test    (required)The condition you want to test.
value_if_true    (required)The value that you want returned if the result of logical_test is TRUE.
value_if_false    (optional)The value that you want returned if the result of logical_test is FALSE.

For more information about the usage of this function, refer to the Microsoft documentation.

International Open Academy

Add a new Switch activity and give the if condition in place of the expression.

In the properties panel, change the argument type to String.

Add new case Passed without double quotes because we have already changed the argument type to string. In this case, add the activities you want to perform. I am just using a log message activity.

Similarly, add another new case Failed. You can add a new case for Error as well. But I will be using the default for it. Default is used when none of the cases is satisfied.

Here is how the final flow looks like.

Test the flow by running the process.

Output when message is passed
Output when message is some error occurred

Thank you All!!! Hope you find this useful.

If you liked our content and it was helpful, you can buy us a coffee or a pizza. Thank you so much.


International Open Academy

Up ↑

%d bloggers like this: