Set Variable Switch Fail

I have a large switch statement with about 20 case values. Switch is on an integer value in a SP list … I have tried using
items(‘For_each’)?[‘buID’]
and also string(items(‘For_each’)?[‘buID’])

It seems to evaluate the case statement and then attempts to set the variable valuable to long string (that is a manual value of a GUID). It is not dynamic.


Screenshot 2024-09-01 074209

I then get this error.

I tried using “test” as the set variable value instead of the guid string and still got the error. It does match on the case of equals 64 in this case.

Thoughts?

I had wondered if I needed to convert the integer to a string value prior to the case stmt??

Can you show more of your flow to include the switch case?

I believe it’s due to a type mismatch. Switch actions are not good at determining string vs integer.

Try something silly like this:

int(string(items(‘For_each’)?[‘buID’]))

1 Like

Personally I would use a completely different approach. The idea of a switch statement with 20 branches does not seem tidy to me. Given that you are only trying to set the value of a variable and they are hard coded values I would start with a JSON array in a compose action, something like this:

[
    {
        "BUID": 23,
        "Value": "SomeValue"
    },
    {
        "BUID": 64,
        "Value": "Test"
    }
]

Then I would add a filter array action that filters the array based on the BUID, so for example 23. Next add a compose action that takes the first (and only) result of the filter array action and extracts the Value. So a simplified version would look something like this:

You would have your Business Units Array outside of your apply to each and your filter array within an apply to each.

This is a much cleaner approach. You could enhance it further by creating a list which contains the BUIDs and values and therefore remove the hardcoding of the Business Units array.

Does that make sense?

1 Like

Yes, it does Paulie. I like that idea because even navigating a 20 wide switch statement is ugly and a pain too. Plus it likely solves the weirdness around the string vs. int issue that Matthew mentions. I already have those BU’s in a List Rows call to DV.

Thank you both for your feedback.

1 Like