web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Catching an Error when...
Power Apps
Answered

Catching an Error when calling the Patch function inside a loop

(0) ShareShare
ReportReport
Posted on by 62

Hello.

 

I have a button inside my app that I am using to send all the rows from a Collection to a Table located in a MySQL database. I am trying to implement a way to catch any errors that might happen in the process of sending the data (missing Internet connection, etc...) so that I can tell the user to try to send the data again.

 

Here is the code that I am currently using for that:

 

MySQLTable contains columns (ID, Field1, Field2)

MyCollection contains columns (tempID, tempField1, tempField2)

 

ForAll(MyCollection,
 Patch('MySQLTable', Defaults('MySQLTable'),
 {
 ID: tempID,
 Field1: tempField1,
 Field2: tempField2
 }
 );

 // Check if there were any errors when the data was submitted
 If(!IsEmpty(Errors('MySQLTable')),
 Collect(MyCollectionBackup, Filter(MyCollection, tempID = 'tempID'));
 );
);

 

My idea is that at the end of each iteration of the For loop, a row from MyCollection will be sent to MySQLTable. If there were any errors trying to do that, then this row should be stored in MyCollectionBackup. At the end of the loop, MyCollectionBackup should contain all the rows that failed to be sent, so that the user can try to send them again later.

 

Can anybody please tell me if I'm doing this correctly, or if there is any better way to achieve this?

 

Thanks in advance.

Categories:
  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @linx9 

    Very programmatic!  PowerApps is not a development language, so things like the ForAll are not what you expect.

    You are trying to use it like a ForLoop in some development language.  ForAll is a function that returns a table of records based on your iteration table and record schema.

    It is more efficient to use the function as intended and will provide better performance.

    Your current formula will take long amounts of time to perform.

     

    If you are attempting to "catch" errors during the iterations, and then store them someplace else for later, you will not be able to avoid the actual errors that are going to appear for the user.

     

    You are better off checking the internet connection prior to performing the formula function.

    Ex. 

    If(Connection.Connected,
     Patch('MySQLTable', 
     ForAll(MyCollection,
     {
     ID: tempID,
     Field1: tempField1,
     Field2: tempField2
     }
     )
     )
    )

     

    You can then check the Errors table after the above formula to see if there are any errors from the function and then adjust accordingly.  But, this will still not reduce any errors that the user will see.

     

    I hope this is helpful for you.

  • linx9 Profile Picture
    62 on at

    Hi @RandyHayes 

     

    I was not aware that the ForAll function worked like that, so thanks a lot for the information. That explains why my app was not running very fast.

     

    I will make sure to reimplement and run some tests with the new code.

     

     

  • linx9 Profile Picture
    62 on at

    @RandyHayes 

     

    After running a few tests, I'm getting the following error when trying to nest the ForAll function inside the Patch function as you suggested:

     

    "Network error when using Patch function: The specified record was not found"

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @linx9 

    This means that you are providing a record with a primary key (assumed to be ID in your case) that does not exist.

    Make sure you are providing valid record keys.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 243 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 190 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 135

Last 30 days Overall leaderboard