Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Unanswered

Why does this "With"/"If" construction fail?

(0) ShareShare
ReportReport
Posted on by 241

Sorry for raising yet another mysterious issue. I'm breaking my head over a problem I'm running into in my app.

 

I'm writing a handler for the 'OnCheck' event of a toggle switch.  I was able to analyze it down to the following simple example: 

UpdateContext({ connectioncheck: Connection.Connected, patch: true } );
With({ name: "Bill" },
 If( connectioncheck,
 With( { something_else: "" },
 If( patch,
 Notify("patching");
 //Patch(Users, Defaults(Users), { 'Full Name': name } );
 ,
 Notify("not patching");
 );
 );
 ,
 Notify("collect");
 //Collect(dummycollection, { fullname: name });
 );
);

This code will not do a lot yet, but is considered correct. 

You will notice I commented out 2 lines. Those are required for the code to actually do what I want. However, when any (or both) of those lines are uncommented, the editor starts throwing errors (mostly simply saying 'The function If has some invalid arguments').

I don't see what's going wrong here. I've used nested "With" constructions in more places in my app and never ran into this problem?

  • v-xida-msft Profile Picture
    on at
    Re: Why does this "With"/"If" construction fail?

    Hi @MrNappa ,

    Could you please share a bit more about the error message within your app?

    Could you please show more details about the "Users" data source?

     

    Based on the issue that you mentioned, I have made a test on my side, and don't have the issue that you mentioned. Please consider modify your formula as below:

    UpdateContext({connectioncheck: Connection.Connected, patch: true});
    With(
     {name: "Bill"},
     If(
     connectioncheck,
     With(
     {something_else: ""},
     If(
     patch,
     Notify("patching");Patch(Users, Defaults(Users),{'Full Name': name}),
     Notify("not patching")
     );
     ),
     Notify("collect");Collect(dummycollection, {fullname: name})
     )
    )

    For Patch function in above formula, please check if you have typed proper value for Required field in your Users if there are some Required fields existed in your Users data source.

    Note: If the Users data source is a SP List data source, please make sure you have specified Required field (e.g. Title) from your data source within the '{}' part of the Patch function.

     

    You could consider move above formula from the OnCheck property into the OnChange property of the Toggle control, then check if the same issue would show up.

     

    Also please consider turn on the "Formula-level error management" option within Advanced settings of App settings of your app, then try your formula again, check if the issue is solved.

     

    Best regards,

  • CarlosFigueira Profile Picture
    on at
    Re: Why does this "With"/"If" construction fail?

    Thank you for reporting this issue; I'll file a bug with the appropriate team to get it fixed.

    Meanwhile, the issue seems that, inside a With statement, the If function is requiring both branches (the "true" branch and the "false" branch) to have the same type, so you can work around this issue by having a dummy value that is returned by it:

    UpdateContext({ connectioncheck: Connection.Connected, patch: true } );
    With({ name: "Bill" },
     If( connectioncheck,
     With( { something_else: "" },
     If( patch,
     Notify("patching");
     Patch(Users, Defaults(Users), { 'Full Name': name } );
     ""
     ,
     Notify("not patching");
     );
     );
     ,
     Notify("collect");
     Collect(dummycollection, { fullname: name });
     "";
     );
    );

    Hope this helps!

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,668 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,004 Most Valuable Professional

Leaderboard