cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Advocate IV
Advocate IV

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

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?

2 REPLIES 2
Highlighted
Power Apps
Power Apps

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!

Highlighted
Community Support
Community Support

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,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Community User Group Member Badges

FIll out a quick form to claim your community user group member badge today!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (7,916)