How to use the UiPath Switch activity

Conditional logic is most commonly implemented through the use of an if-then-else statement. However, when an RPA developer is faced with multi-branched logic based on a single condition, an if activity can become unwieldly, especially if the nesting of if statements goes beyond a second level. In these situations, a better option is to use a conditional operator that supports multiple branches. That is what we will explore in this UiPath Switch activity example.

To use a UiPath Switch activity in an RPA project, follow these steps:

  1. Add the UiPath Switch activity from the Activities tab.
  2. Set a variable to act as the switch’s expression.
  3. Add a case for each possible expression match.
  4. Add an optional default if there is no switch case that matches.
  5. Implement the appropriate logic for each case.
A strategy to learn UiPath Orchestrator
There are five phases involved in order to create, deploy and manage RPA robots at runtime:

  • An RPA program must be developed in UiPath Studio. In this ongoing example, I use the UiPath Hello World app created in a previous tutorial.
  • A UiPath Orchestrator robot must be created, which is demonstrated here.
  • The UiPath Assistant must be configured so projects written in UiPath Studio can be published to UiPath Orchestrator.
  • A UiPath orchestator process must be created to associate RPA programs with the robot that runs them.
  • UiPath triggers can optionally be set to schedule the execution of unattended robotic RPA processes.

String as a UiPath Switch expression

You will notice that the expression used in the UiPath Switch example defaults to an int32. This can be changed to a String, boolean value or any other reference type by configuring the expression type in the switch activity’s properties panel.

This UiPath Switch example follows through a hypothetical situation where an int32 variable named players is passed to the activity’s expression. Depending on how many players are available, the application tells the user the best matching sport to play:

  • With 11 players, there is enough for a cricket team.
  • With 15 players, a hurling team could be assembled.
  • With 6 players, a hockey team could be formed.
  • If there is no match, the user is told to take everyone surfing.
UiPath switch example

This UiPath Switch activity examples shows an alternative to nested if statements.

UiPath Switch vs. If

As you can see, this UiPath Switch example implements the required logic in a clean and methodical way. If this was to be implemented with a conditional UiPath If activity in UiPath Studio, the code would be messy, visually unappealing and difficult to maintain and troubleshoot. When the situation demands it, the UiPath Switch activity is an excellent way to implement conditional logic.