Skip to main content

Notifications

Power Automate - General Discussion
Unanswered

Switch Case not working with setting variables

(0) ShareShare
ReportReport
Posted on by

I am trying to set a variable in a switch statement. It is grabing an substring from a URL and set a variable called Set company. The switch statement is failing with an "unexpected error".

 

Screen Capture

  • user301913 Profile Picture
    user301913 2 on at
    Re: Switch Case not working with setting variables

    Commenting because I had a very similar situation (was looking for the first character of a field to see and switching on its value [which included 1, 2, 3, and 4 as valid cases]). Rather than add steps or rewrite, I took the lazy way and added single quotes around both the switch and every case. Works well and seems to be stable.

     

    user301913_0-1659034908829.png

     

  • FlowUser1 Profile Picture
    FlowUser1 29 on at
    Re: Switch Case not working with setting variables

    Thank you for taking the time to do the detailed explanation.
    your tip of adding the compose and a non numerical character in the compose and the cases of the switch solved this cryptic error 

  • damuqam Profile Picture
    damuqam 6 on at
    Re: Switch Case not working with setting variables

    But it seems that the magic is happening in the switch step where it seems to set the data type from its content at input, while its cases are handled as strings. I then found that adding a non numeric value to the end of the comparison to the switch input will make it interpret it as a string instead of a number datatype. you will also have to add this non-number character at the end of the cases as well.
    This seems to be a workaround to the issue.
    force string type of inputforce string type of input


    I had a similar problem where I couldn't define a variable inside a switch because its on parameter was a number (in a string). Instead of adding the Compose action like you did, I simply added a caracter to the on and the equals parameters of the switch.

    Capture d’écran, le 2021-01-25 à 10.43.48.png

    Works perfectly now. Thanks @hjaf .

  • dfknowles Profile Picture
    dfknowles 3 on at
    Re: Switch Case not working with setting variables

    Thank you for the tip.  Using Compose solved the issue I was having with the Switch command. 

  • hjaf Profile Picture
    hjaf 486 on at
    Re: Switch Case not working with setting variables

    I just came across this issue as well and perhaps identified where the issue is.
    @v-yamao-msft  used a substring during testing, and that is part of the key here. The real issue is that when you create a trigger input and select it to be a text, or a selection of a text you expect this to be a string. That is not the case when using the trigger in switch it seems, providing a somewhat non-descript error message. This seems to be because switch is setting the datatype on the disregarding information from the trigger body: 

     "text_1": {
     "title": "Master",
     "type": "string", <----
     "x-ms-dynamically-added": true,
     "description": "Select master year",
     "x-ms-content-hint": "TEXT",
     "enum": [
     "2020",
     "2021"
     ]
     }


    if I supply the direct, or even indirect value from the trigger (button) it does not work, but if i replace this with a text string at design time its no issue.  (Images show using compose but these have been tested both directly and indirectly with compose.)
     
    manually input a value to the compose used in switchmanually input a value to the compose used in switchmanually input a value to the compose used in switchmanually input a value to the compose used in switchinput from a button text field failsinput from a button text field failsinput from a button text field failsinput from a button text field fails

    I Thought perhaps that the solution would be to adding a string() expression on what is expected to be a string in the first place as the field is actually named a text field regardless if you are using it in a dropdown or selection. 
    workaround with using string()workaround with using string()

    But it seems that the magic is happening in the switch step where it seems to set the data type from its content at input, while its cases are handled as strings. I then found that adding a non numeric value to the end of the comparison to the switch input will make it interpret it as a string instead of a number datatype. you will also have to add this non-number character at the end of the cases as well.
    This seems to be a workaround to the issue.
    force string type of inputforce string type of input

  • v-yamao-msft Profile Picture
    v-yamao-msft on at
    Re: Switch Case not working with setting variables

    Hi @Anonymous,

     

    If you are still having the issue, please share a full screenshot of how is your flow configured to help reproduce this issue on my side.

     

    Best regards,
    Mabel Mao

  • Re: Switch Case not working with setting variables

    The switch statement looks tests for a specific number and then sets the variable to a string.  

  • v-yamao-msft Profile Picture
    v-yamao-msft on at
    Re: Switch Case not working with setting variables

    Hi ericramos2017,

     

    Could you show me a detailed screenshot of your flow configuration to help reproduce this issue?


    To test this issue, I created a flow likes below.


    The actions “Set variable” run succeeded under the Switch cases.

     

    Please take it for a reference and try again with it. If you are still having the issue, please feel free post back with more details.

    3.PNG

    4.PNG

     

    Best regards,
    Mabel Mao

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,567

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,907

Leaderboard