The Switch is one of the steps in Power Automate that 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.
The Coalesce function
Within the switch don’t use the Choice Value, but use the Choice value inside a coalesce function:
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!)
Other Switch Posts
14 thoughts on “Improve your switch case with coalesce in Power Automate”
Nice, Coalesce and Unknown for Switch/Case.
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. 🙂
Can you do this with a boolean response? I tried this, it pushes through an unknown response every time.
Yes you can use boolean values in select statements. Can you email me a screenshot of your select?
Was just looking for this , you saved my bacon again thanks Pieter !
I need to find a way to route the emails from the Shared email box to SharePoint Online libraries and the issue is i need to use the “Contains” condition of the email parameters say Subject or the attachment name but i had a limitation in switch condition where we cannot use the contains condition.
is there any work around for the above ? Please find the flow screen shot below.
I had a similar thing in the past where an order type could be found in the email. I first got the order type and then used this within the switch rather than the contains.
Thanks for the reply Pieter, but the subject of the email is always unique alpha numeric string and it gets changed in every email.
For suppose I stored the subject in a variable as a string and in the next step of switch statement if i use the variable that would be the same and doesn’t make any difference as this would not decide to store in which SharePoint library.
Ok. Can you give some examples of how the subject is used to make the choice.
This didn’t quite work for me..The failure error i get is because it is Timing Out, not because it is Null. When an email with options times out, the result is null but that is not the selected option. What could I use in this case?
Can you post your coalesce expressions that you are using?
This worked to resolve my null error message as well. Thanks for posting!
Thanks for this, what happen if you have two choice columns, like country and city. and users can leave both blanks
It is unlikely that you would use both of those choice columns in a switch. A switch checks a single value and then offers branches of steps. If you wanted to have branches of steps for for example country and city then only the city location is really relevant. So you could probably use the city as your switch for the branches rather than both choices.