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)

Example: To Submit the Form the following power apps formula is written on the OnSelect property of the Button control.

PowerApps Formula

SubmitForm(Form1); NewForm(Form1)

After submitting the form, the form mode again changes to the NewForm mode so in order to fill the form again.

SubmitForm function in Power Apps

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.

SubmitForm function in Power Apps

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.

SubmitForm function in Power Apps

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

Notify("Data is saved successfully. Thank You.")
SubmitForm function in Power Apps

We can write the following formula on OnSuccess property of Form control.

PowerApps Formula

Navigate(Screen2, ScreenTransition.Cover); Notify("You have successfully submitted a record for " & Self.LastSubmit.‘Teacher Name’)

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.

SubmitForm function in Power Apps

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.

SubmitForm function in Power Apps

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

Notify("Your form is not submitted. Please fill all the required fields.", NotificationType.Error)
SubmitForm function in Power Apps

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.

SubmitForm function in Power Apps

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.

SubmitForm function in Power Apps

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

ResetForm(Form1)

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.

SubmitForm function in Power Apps

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

Notify(“Your Form is Reset successfully.”)
SubmitForm function in Power Apps

When we click on the Reset button, the form is reset and we are notified also as shown in the following image.

SubmitForm function in Power Apps

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

FormName.LastSubmit

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

Form1.LastSubmit

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

SubmitForm(Form1)
SubmitForm function in Power Apps

Suppose on the first form, we have added the details, and click on Submit. The record is saved and we are notified also.

SubmitForm function in Power Apps

We can access the last successfully submitted record.

SubmitForm function in Power Apps

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.

SubmitForm function in Power Apps

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.