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
Community Conference

Power Platform Community Conference

Find your favorite faces from the community presenting at the Power Platform Community Conference!

Watch Now

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

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,235)