
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.