Microsoft Flow – Improve your switches

One of the steps in Microsoft Flow can cause a lot of failures and it might not always be clear why things are going wrong. In this post I’m going to have a look at the Switch.

I created a list with a choice field. The field is not mandatory and therefore it could be left blank. I then created my flow that triggers on the creation of a new item and a switch that responds to my choice field.

then I created a new list item and selected Choice 1 as my value for my choice field.

All good so far, but now what happens if you don’t select anything and the choice field remains empty. You might expect (or hope) that the default branch is followed.

 

But that is not the case. Your flow will fail with an ugly:

The execution of template action ‘Switch’ failed: The result of the evaluation of ‘scope’ action expression ‘@triggerBody()?[‘Choice’]?[‘Value’]’ is not valid. It is of type ‘Null’ but is expected to be a value of type ‘String, Integer’.

 

One way to get around this is to check before the switch if the choice field has been set or not and then run the switch only when it is set. If this is your approach then “have you noticed that your flows become large?”

There is a better way around this.

Within the switch don’t use the Choice Value, but use the Choice value inside a coalesce function:

coalesce(triggerBody()?['Choice']?['Value'],'Unknown')

The Coalesce function will return either the value of the Choice or if that is not set, ‘Unknown’ will be returned.

And now your flow runs without failure (Well at least your switch doesn’t fall over!)

 

 

 

Advertisements


Categories:Flow, Microsoft Flow, Office 365

Tags: , ,

4 replies

  1. Nice, Coalesce and Unknown for Switch/Case.

    Like

  2. I’m working for HR and I just wanted to say BIG/HUGE THANKS! this has helped me enormously to keep my flow working without that ‘Null’ error message. 🙂

    Like

  3. Can you do this with a boolean response? I tried this, it pushes through an unknown response every time.

    Like

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: