web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Error when Switch, If ...
Power Apps
Unanswered

Error when Switch, If and Patch combined

(0) ShareShare
ReportReport
Posted on by 28

Hi everyone,

I came across 2-3 topics about this issue but solutions were based either on luck or vague understanding. 

So please, enlighten me. 

I'm trying to combine a patch within an If statement within a SWITCH statement however it looks like it is not allowed.

 

 

 

Switch(
 _CurrentStep;
 5;
 NewForm(form_1) && UpdateContext({_showForm_1: true});
 2;
 If(
 DataCardValue_retour.SelectedText.Value = "Oui";
 And(
 UpdateContext({_StepID: _CurrentStep + 1});
 UpdateContext(
 {
 _StepValue: LookUp(
 'SPlist1';
 ID = _CurrentStep + 1;
 Title
 )
 }
 );
 SubmitForm(Form2);
 Refresh('SPlist1')
 );
 Patch('SPlist1';_Regtraite;{Complete:true})
 );
 UpdateContext({_StepID: _CurrentStep + 1}) && UpdateContext(
 {
 _StepValue: LookUp(
 'SPlist1';
 ID = _CurrentStep + 1;
 Title
 )
 }
 ) && SubmitForm(Form2) && Refresh('SPlist1')
)

 

 If I replace the line Patch('SPlist1';_Regtraite;{Complete:true}) with "" it works well so I guess the issue comes from the patch function within an if statement. 

- This code is put in the Onselect of a button

- all the variables start with a "_"

- I've tried the Patch line alone in another button, it works fine also. 

 

If you are able to help, thanks a lot !

Categories:
I have the same question (0)
  • PowerRanger Profile Picture
    3,458 Super User 2024 Season 1 on at

    @FrobCol I am not able to go thru your complete code as I am on my mobile currently. But Patch() will return a record not a String. What are you trying to achieve? Could you describe it in more detail?

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

  • FrobCol Profile Picture
    28 on at

    Hi @PowerRanger, thank you for the reply. Let me try to tell you what I want to do with words.

    First, I am a french user so my code has semicolons ";" instead of commas ",".

    This code is applied to a button that will Switch to different actions depending on the value of the _currentStep variable - (integer value between 1 and 10) - :

    - If _currentStep = 5 then create new form "form1" and change the variable _showform_1 = true

    - If _currentStep = 2 Then :

                                                - If datacardvalue = "Yes" then apply defaut action of switch statement

                                                -  If datacardvalue = "No"  Then Patch a boolean value in SharepointLIst1 for the record "_regtraite"

    - If _current step = any other value then apply defaut action of switch statement.

     

    I worked with this same code before, i've just recently add the part where "_currentStep = 2" so i guess the part where _currentStep = 5 is working fine and the defaut action too.

     

  • Verified answer
    FrobCol Profile Picture
    28 on at

    Hi again, so I found the solution.

    I read again the documentation there : 

    https://docs.microsoft.com/fr-fr/powerapps/maker/canvas-apps/functions/function-if

    And I discovered that if I want to have multiple condition with no relationship with each other I should only use IF statement. It doesn't works well to mix Switch and If. Here is my new code.

    Edit : the thing is that I thought that if statement was like in excel : IfConditionThenResult [, DefaultResult ] ) but in powerapps it can also be like : IfCondition1ThenResult1 [, Condition2ThenResult2, ... [ , DefaultResult ] ] )

     

    If(
     _CurrentStep = 5;
     NewForm(form_1) && UpdateContext({_showform_1: true});
     _CurrentStep = 2 && DataCardValue_retour.SelectedText.Value = "Non";
     Patch(
     'Splist1';
     _Regtraite;
     {Complete: true;'Date de clôture':Now()}
     );
     UpdateContext({_StepID: _CurrentStep + 1}) && UpdateContext(
     {
     _StepValue: LookUp(
     'Splist2';
     ID = _CurrentStep + 1;
     Title
     )
     }
     ) && SubmitForm(Form2) && Refresh('Splist1')
    )

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard