Table of contents:
The Topic: Check required fields before using the Patch function in Power Apps
Often in Canvas apps, instead of a Form, we use a save button with the Patch() formula for submitting data to a data source. But then the question arises, how can we define mandatory fields and verify them before the data is submitted? And how can we provide feedback to the user on which mandatory fields have not yet been filled? Exactly this topic will be discussed in the following.
I will show you how to get the following result (see screenshot):
Step 1: The formula on the save button
The save button contains the following formula on the OnSelect property:
//1: The auxiliary variable (VarRequired) that records whether there was a required field violation is set to false in the beginning, before the check.
Set(
VarRequired,
false
);
//2: Check required fields: It checks if the required fields are blank, if so, the auxiliary variable VarRequired is set to true and a notification is shown to the user.
If(
Or(
IsBlankOrError(TextInput_AccountName.Text),
IsBlankOrError(TextInput_City.Text)
),
//Required fields NOT OK
Set(
VarRequired,
true
);
Notify(
"Please fill in all required fields",
NotificationType.Warning,
4000
)
,
//Else: required fields OK:
//3: Check if VarCurrentAccount (Record variable) is Blank(): If true, add new Account, else update Account.
If(
IsBlankOrError(VarCurrentAccount),
//Patch NEW and set the result as the new VarCurrentAccount
Set(
VarCurrentAccount,
Patch(
Accounts,
Defaults(Accounts),
{
'Account Name': TextInput_AccountName.Text,
Email: TextInput_Mail.Text,
'Address Phone': TextInput_Phone.Text,
'Address 1: City': TextInput_City.Text
}
)
),
////Patch UPDATE and set the result as new VarCurrentAccount
Set(
VarCurrentAccount,
Patch(
Accounts,
VarCurrentAccount,
{
'Account Name': TextInput_AccountName.Text,
Email: TextInput_Mail.Text,
'Address Phone': TextInput_Phone.Text,
'Address 1: City': TextInput_City.Text
}
)
)
)//end if IsBlank VarCurrentAccount (New/Update Patch)
)// end required fields check
The blue marked terms in the formula should be replaced by your own values.
What is the logic of this formula?
The auxiliary variable that records whether there was a required field violation is set to false in the beginning, before the check.
The auxiliary variable (VarRequired) that records whether there was a required field violation is set to false in the beginning, before the check.
It checks if the mandatory fields are blank, if so, an auxiliary variable VarRequired is set to true and a notification is shown to the user.
To determine whether a new record should be created or an update should take place with the Patch() formula, it is checked whether the "record variable" is empty. I often use this concept with such detail screens without forms.
Step 2: The color of the label next to the mandatory field
The label next to the required field contains the following formula on the Color property:
If(IsBlankOrError(TextInput_City) And
VarRequired=true,Color.Red,
RGBA(0, 0, 0, 1))
That is, the label turns red if the textinput control is empty and at the same time the previously defined variable VarRequired is true. Otherwise the label remains black.
Step 3: Resetting the auxiliary variable
In order for the auxiliary variable VarRequired to be defined as false by default, we set it accordingly in the OnVisible property of the screen.
Set(VarRequired,false)
This is it. Now you can test what you have developed.
Conclusion
In Power Apps there are always different ways and approaches to achieve a goal. I'm eager to hear feedback on what you would do differently and maybe even easier.
Do you like this article?
Please write a comment or leave a like below and follow me on LinkedIn.
If you have any questions, I'm happy to help.
Marc Sigrist (Powerfully GmbH, Switzerland)