SubmitForm and ResetForm functions
The SubmitForm function is used to in two ways:
a) When the form is in Edit mode The function edits the existing record information and update in the already existing record in the data source.
b) When the form is in New Mode The function creates a new record from the information that the user entered in the Form control to the data source.
The function is often triggered on the OnSelect property of a Button control.
Power Apps Syntax SubmitForm(FormName)
- FormName – It is a Required parameter. It specifies the name of the Form control to submit to the data source.
Example: To Submit the Form the following power apps formula is written on the OnSelect property of the Button control.
PowerApps Formula
After submitting the form, the form mode again changes to the NewForm mode so in order to fill the form again.
Each data card on our Form control has an Update property where the value or formula that will be used to update that column is specified. If we have unlocked our card, we can modify that formula. This is often unnecessary and only done in special circumstances.
What happens when the SubmitForm function triggers?
Before submitting any changes to the DataSource, the SubmitForm function checks for validation issues with any field that is marked as required or that has one or more constraints on its value.
The Form control has three properties that process after the data is submitted based on the outcome of the submission. These properties are OnSuccess , OnFailure , and OnReset .
1. OnSuccess property of the form control The OnSuccess property of the form control specifies the actions to perform when a data operation has been successful. This property applies only to the Edit form control. If we want the user to go to a different screen after their data is submitted successfully, then in OnSuccess we would use the function Navigate(SuccessScreen, ScreenTransition.Cover) to send them to the screen named SuccessScreen.
Note: Do not use Navigate just after the SubmitForm because even then the SubmitForm function throws error still the Navigate function executes. So always use Navigate in the OnSuccess Property of the Form control.
By default, the OnSuccess property value is false.
We can set the OnSuccess property of the form to the following formula to notify the users that their data is successfully saved.
PowerApps Formula
We can write the following formula on OnSuccess property of Form control.
PowerApps Formula
After filling all the required fields, click on Submit button. After all validation checks, when the form data is successfully loaded in the data source only then we are notified by a message as shown in the following image, otherwise not.
2. OnFailure property of a Form control The OnFailure property specifies the actions to perform when a data operation has been unsuccessful. This property applies only to the Edit form control. By default, its value is false.
We can use the OnFailure property on the form control if there is an error when the data is submitted. We could use a formula to specify a warning message that appears when the failure occurs. The following formula is an example of using the Notify function to provide more info to the user.
PowerApps Formula
This function would display a red warning at the top of the screen with the message "Your form is not submitted. Please fill all the required fields." This message makes it easier for the user to resolve the issue.
3. OnReset property of a Form control
We can use the OnReset property if the form is reset. A form reset occurs when the ResetForm function is used. The ResetForm function sets the form back to its default values and then runs any formula specified in the OnReset property.
By default, its value is false.
Let us Reset the form by using the ResetForm function. First add a button control and change its Text property value to Reset and write the following formula on the OnSelect property of the button control.
PowerApps Formula
Here, Form1 is the name of the form control in Power Apps which we are going to reset.
The ResetForm function resets the contents of a form to their default values, and any user changes are discarded. The OnReset behavior of the form control also runs. We can also reset individual controls with the Reset function but only from within the form.
At this point when we click on the Reset button, we don’t receive any notification, but the form is reset. So, the form is reset but the user is not notified.
Let us notify the user when the form is reset, by writing the following formula on the OnReset property of the form control.
PowerApps Formula
When we click on the Reset button, the form is reset and we are notified also as shown in the following image.
4. LastSubmit property of a form control
The LastSubmit property specifies the last successfully submitted record, including any server generated fields that means we can access entire record. This property applies only to the Edit form control.
We can access the record by using the following formula:
PowerApps Formula
It is assuming our Form control was named FormName, and set it to the Item property.
Add another form control and specify it to the same datasource and set its Item property to the following formula.
PowerApps Formula
Also update the OnSelect property of the Submit button on the first form to the following formula. Note we are removing the NewForm function from here.
PowerApps Formula
Suppose on the first form, we have added the details, and click on Submit. The record is saved and we are notified also.
We can access the last successfully submitted record.
We can also access the individual value by inserting a Label control into the app, and then using the formula FormName.LastSubmit.ID to return the ID column value of the last record submitted using the Form control named FormName, and set it to the Text property of the Label control.
5. Error property
The Error property contains the output of any error messages that are generated by the Form control. To view the contents of the property, add a Label control to the screen and then for the Text property enter the formula Form1.Error. It will be blank if there is no error, but if you try to submit a form without entering all of the required columns you would see a message such as "An entry is required or has an invalid value. Please correct and try again." You can use this value in formulas or other controls to design our apps error handling.