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 / Example Patch with con...
Power Apps
Unanswered

Example Patch with condition and error handling

(0) ShareShare
ReportReport
Posted on by 41

I've been struggling with a patch function (and it's documentation). But I managed to make it work while trying to explain what didn't work in this very post. So instead of a question and wait for answers, I'll write the answer right away.

 

I've used the code from this page directly: https://www.matthewdevaney.com/power-apps-patch-function-error-handling/

And the answers in this post guided me to the solution: https://powerusers.microsoft.com/t5/Building-Power-Apps/How-do-I-Access-the-returned-value-from-a-patch-function/td-p/39856

 

The trouble I had was introduced when I added my field checking. Because I didn't want any patching to happen if required fields were missing. I refreshed the datasource dozens of times, thinking the error wasn't returned. Which in the end I realized was true, because it skipped the clearcollect part of the code.

It seems that all collections created by clearcollect are there at the initialization of the form, no matter where they are in the code. The code below relies on that to be true, because even if the If statement is false, the collection is created, making it countable in the second part of the code. I still think how that functions is a bit strange though, I wouldn't be surprised if that changes in the future.

 

If(someCondition = true,
	ClearCollect(MyPatchedRecords,
		Patch(
				'Test Scores',
				Defaults('Test Scores'),
				{
					TestName: txt_TestName.Text,
					StudentName: txt_StudentName.Text,
					Score: Value(txt_Score.Text)
				}
			)
		),
		"If false, do nothing"
	);
	
 If(
 // Check if there were any errors when the test score was submitted
	 // Check if any patching happened at all. The if condition around the clearcollect function
	 // will prevent an error from happening.
 !IsEmpty(Errors('Test Scores')) Or CountRows(MyPatchedRecords) = 0,
 // if true, show any error message
 Notify(
 Concat(Errors('Test Scores'), Column&": "&Message),
 NotificationType.Error
 ),
 // else, go to success screen
 Navigate('Success Screen');
 )

 

 

Anyway, I hope this is helpful to someone.

 

 

Categories:
I have the same question (0)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 534

#2
WarrenBelz Profile Picture

WarrenBelz 416 Most Valuable Professional

#3
Valantis Profile Picture

Valantis 306

Last 30 days Overall leaderboard