cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MrNappa
Kudo Collector
Kudo Collector

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
CarlosFigueira
Power Apps
Power Apps

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!

v-xida-msft
Community Support
Community Support

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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power Apps Community Call Jan. 2022 768x460.png

Power Apps Community Call

Please join us on Wednesday, January 19th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (1,473)