cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper III
Helper III

Check that a ForAll Patch was successful

Hi, I have an app that patches data from a collection into the CDS.  I accomplished that by using a button control.  Since I can have multiple records in a collection, I use the ForAll expression:

 

ForAll(CollectionA, Patch(Table1, Defaults(Table1), { Field1: Field1, .... Field12: Field12} ))

 

I want to make sure that the patch works, that the data is copied to the CDS table.  Maybe somehow there is a network issue or mobile phone issue that intterrupts the transfer, I want the user to know that, so they can try again.  I have read that this could be accomplished by using the Errors function along with the IsEmpty or IsBlank Functions.  But all the examples i read about is for when using a Patch only.  How can I accomplish this if I am using an ForAll patch?  Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Helper III
Helper III

Re: Check that a ForAll Patch was successful

Hi @FernandoTC ,

 

I finally had a chance to try out your code.  I was pulled away for a different project the last couple of weeks and finally gotten a chane to get back to powerapps.  First i have to say, that was tough conceptially, but i was finally able to get myself to wrap my brain around it.  The code does work, but as you said it does not work in the browser but it did work in my mobile powerapps app.  There is another problem that I saw from this, and i'm hoping you can help me.  Using the Concurrent function, after the ForAll Patch and the successful Notify message, I also added the Clear and Set statements, but there is one particular Clear that is giving me errors and I do not understand why because I am already adding two seperate clear on two other collections.  And I am trying to clear the main collection that actually is the data that patches to the CDS.  But when I add this one, it gives me "The function 'Concurrent' has some invalid arguments".  Again all three clears are to clear a collection.  

 

The second question is, what would normally cause a patch to fail that would invoke the default action of the IfError function.  I'd like to try it to test that scenario.  Thanks again for your help. I am going to check this as a solution.  

 

 

View solution in original post

7 REPLIES 7
Highlighted
Resolver II
Resolver II

Re: Check that a ForAll Patch was successful

Hi @mahakala_

 

If something goes wrong during the forall loop, the user will get an error notification at the top of the app. The problem is that this notification will be messy as the common user will not understand it. You can check if a function is having a runtime error by using the function IsError() or perform an action if the function fails during the runtime by using IfError().

 

Try this:

IfError(
          ForAll(
          CollectionA,
          Patch(
                     Table1,
                     Defaults(Table1), 
                     { 
                       Field1: Field1,
                          .... 
                      Field12: Field12
                       } 
                     )
             ),
Notify(
        "Something went wrong",
        NotificationType.Error
    ),
Notify(
        "Success",
        NotificationType.Success
    )
)         

This will show a personalized success message or error mesage to warn the user if the App worked successfully or not.

 

Hope this helps you!


 

Highlighted
Helper III
Helper III

Re: Check that a ForAll Patch was successful

HI @FernandoTC ,

 

Thanks for your help.  It works, but not what I fully intend it to do.  It does not seem to work in failed network situations.  It does not give the error message, in the user interface, but it will give you a warning in the control.   I figured a network failed would be one of the top reasons why a patch would fail.  

 

Also is there a way where If the ForAll patch went through successfully, I want to clear some collections and reset a variable?  I tried to put an '&&' along the success notify statement, but that didn't work.  So my workaround currently is just to put the clear statements instead of the success notify statement if it patches successfully.  The downside is that I do not get the successfull notification, but I guess I can always navigate it to a different page and display a successfull msg there.   Is this the right approach you think, or do you have other suggestions I can try?  Thanks.

Highlighted
Resolver II
Resolver II

Re: Check that a ForAll Patch was successful

HI @mahakala_ 

Regarding the network failure, you can try using Connection.Connected before the forall statement. This way, it will check your connection before going through the Patching. 
If you want to execute another statement after the forall, you can use two options:

  • Function Concurrent() allows you to execute several formulas at the same time.
  • If you put ; between two functions, once the first is executed completely, the following function will proceed to be executed.

I think the final code you need is the following:

If(
Connection.Connected,
IfError(
Concurrent(
ForAll(
_CollectionA,
Patch(
Table1,
Defaults(Table1),
{Field1: Field1}
)
),
Notify(
"Success",
NotificationType.Success
),
Clear(_CollectionB),
Reset(_ControlName)
),
Notify(
"Something went wrong",
NotificationType.Error
)
),
Notify(
"Unable to connect, please check your network",
NotificationType.Error
)
)

With this syntax, we first check if the Internet connection is ok, if not, we display an error message asking the user to check the connection.

If the connection is ok, we proceed to execute the concurrent function with the forall() statement, the success notify, and the clear or reset statements. If this goes wrong in any way, the IfError() function will display the error message warning the user that something went wrong.

 

I tried this solution and the Connection.Connect statement does not work properly if using the web browser as i losses the connection and cannot continue evaluating anything within the app. However, if you are using PowerApps studio, or the mobile/tablet PowerApps app, it will work as intended.

 

Hope this is what you were looking for.

Highlighted
Helper III
Helper III

Re: Check that a ForAll Patch was successful

Hi @FernandoTC ,

 

Just wanted to let you know I see your reply since Monday but I have not gotten a chance to try it out yet as I was pulled into something else at work.  Once I do try it out I will let you know how it worked out. Thank you for taking your time to help.

Highlighted
Helper III
Helper III

Re: Check that a ForAll Patch was successful

Hi @FernandoTC ,

 

I finally had a chance to try out your code.  I was pulled away for a different project the last couple of weeks and finally gotten a chane to get back to powerapps.  First i have to say, that was tough conceptially, but i was finally able to get myself to wrap my brain around it.  The code does work, but as you said it does not work in the browser but it did work in my mobile powerapps app.  There is another problem that I saw from this, and i'm hoping you can help me.  Using the Concurrent function, after the ForAll Patch and the successful Notify message, I also added the Clear and Set statements, but there is one particular Clear that is giving me errors and I do not understand why because I am already adding two seperate clear on two other collections.  And I am trying to clear the main collection that actually is the data that patches to the CDS.  But when I add this one, it gives me "The function 'Concurrent' has some invalid arguments".  Again all three clears are to clear a collection.  

 

The second question is, what would normally cause a patch to fail that would invoke the default action of the IfError function.  I'd like to try it to test that scenario.  Thanks again for your help. I am going to check this as a solution.  

 

 

View solution in original post

Highlighted
Helper III
Helper III

Re: Check that a ForAll Patch was successful

Hi @FernandoTC ,

 

Just wanted to let you know I was able to add the third Clear statement to clear the main collection without any errors.  So I was messing around with the semi colins.  Initially, I had only one semi colin right after the patch and before the success Notify.  For some odd reason, Clear(_CollectionMain) would give me an error message, but it does not give me errors if I leave that statement out, even though I have two other clear statements in there.  Then I realize it works if I add a second semi colin right after the success Notify statement and before the Clear statements.  It works but I am confused why it works.  See Below code:

 

IfError(
Concurrent(
ForAll(
_CollectionA,
Patch(
Table1,
Defaults(Table1),
{Field1: Field1}
)
);
Notify(
"Success",
NotificationType.Success
);
Clear(_CollectionMain),
Clear(_CollectionB),
Clear(_CollectionC),
Reset(_ControlName)
),

 

And just following up on the second question is, what would normally cause a patch to fail that would invoke the default action of the IfError function.  I'd like to try it to test that scenario.  Thanks again for your help. I am going to check this as a solution.  

 

I will have to revisit how to work around the connection.connected when working in a browser.  

Highlighted
Super User
Super User

Re: Check that a ForAll Patch was successful

Thanks for the IfError gem here @FernandoTC . I've been using PowerApps for a long time and didn't know about this function. I was always of the mindset that "Patch doesn't give you any error handling". Thanks for opening my eyes!

Helpful resources

Announcements
secondImage

New Return to Workplace

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

August 2020 CYST Challenge

Check out the winners of the recent 'Can You Solve These?' community challenge!

Experience what’s next for Power Apps

Join us for an in-depth look at the new Power Apps features and capabilities at the free Microsoft Business Applications Launch Event.

Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Users online (4,409)