cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MShelnutt
Helper IV
Helper IV

How to add an error check to a button function

Hi again.

My PowerApp is not writing to my SharePoint list that is the connected data source.  I discovered the reason is there was an error with one of the fields not being entered correctly.  This error was not caught because the submit button will not only submit the form, but will also launch another powerapp- causing the error to be missed before the launch.  How can I put in an error check before the launch is done?

Currently,

Button2 is made visible when the user chooses "Yes" on the dropdown (Warranty Claim?_DataCard1).  The button is set to SubmitForm(Form1); Launch("web address to another PowerApp edit form").

Button3 is made visible when the user chooses "No" on the same dropdown above.  The button is set to SubmitForm(Form1).

The issues I am having here is after clicking Submit (Button3), I get a pink error across the top saying that Title is required (I dont have a Title field being used).  Also, I am getting an Action error for OnSelect- SubmitForm(Form1).  I dont understand why.

The Form (Form1) is set to ResetForm(Form1) OnSuccess.

 

Thanks,

2 ACCEPTED SOLUTIONS

Accepted Solutions
RandyHayes
Super User III
Super User III

@MShelnutt 

So yes, you can still do this (and absolutely should based on my first message).  You really don't want to do actions that are based on the success of the submit without validating that it was actually a success.

For the one button that should launch, I would add something like: UpdateContext({lclDoLaunch:true});  SubmitForm(yourForm)

Then in the OnSuccess, change the formula to : If(lclDoLaunch, Launch(....))

 

Now, that should solve the first issue of launching even when there is an error.  For the next issue, your SharePoint List has a Title column.  By default, it is required.  You can either change the column to be not required or you can supply a value for the Title column in your EditForm.  That will resolve that issue as well.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

RandyHayes
Super User III
Super User III

@MShelnutt 

I believe you might have misunderstood my point about disabling the buttons.  The purpose was to utilize a formula on the displayMode properties that validates the form before allowing the user to submit.  This avoids executing the submit on an invalid form and thus getting nasty banner errors in the app.

 

If you have no complex validation for your form, then simply put the following into all three of the DisplayMode properties : If(Form1.Valid, Edit, Disabled)

The built in validation of a form will already validate for required fields.

 

If, however, you have more complex validation (I'll use the example that a specific field in the form must be 4 characters long - let's call it AccountCode), then you can extend this out.

Since you are dealing with 3 buttons that should technically not be "available" until the form is complete, I would suggest (instead of copying and pasting the same formula, and then maintaining it, in three places) placing a Toggle control on the screen (let's call it tglFormValid)

In the Default property of the toggle, set the following formula:

 

 

Form1.Valid &&
(Len(Form1.Updates.AccountCode)=4)

 

Now, for the DisplayMode property of the 3 buttons, set the formula to the following:

   If(tglFormValid.Value, Edit, Disabled)

 

You can set the visibility of the toggle to false so it is not seen by users.

When the condition in the default property ( form is valid AND the length of the AccountCode is 4 characters), then the toggle will change to true/on.  The formula in the DisplayMode on the buttons just looks at that toggle value.

Advantage, your validation formula is only in one place.  If you need to change anything, you only do it in one place.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

6 REPLIES 6
RandyHayes
Super User III
Super User III

@MShelnutt 

This is the purpose of the OnSuccess and OnFailure actions of the EditForm.

Move any actions you want to happen when the submit is successful to the OnSuccess action - such as your Launch function.

Also, it is advisable to pre-validate your form information before submitting.  This is usually done by deactivating (displaymode - Disabled) the button where you submit until the form is accurately completed.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

@RandyHayes  I appreciate your time.  I cannot put the Launch function in the Form OnSuccess action, as only one of the submit buttons will do that action.  The other submit button will only submit and reset.

 

I have changed the two buttons' DisplayMode to Disabled.

 

I still have Button 3 with an error for its Action OnSelect being SubmitForm(Form1).  The red X says Title: Field 'Title' is required.  I am at a loss on this.

 

MShelnutt_0-1609947351548.png

 

RandyHayes
Super User III
Super User III

@MShelnutt 

So yes, you can still do this (and absolutely should based on my first message).  You really don't want to do actions that are based on the success of the submit without validating that it was actually a success.

For the one button that should launch, I would add something like: UpdateContext({lclDoLaunch:true});  SubmitForm(yourForm)

Then in the OnSuccess, change the formula to : If(lclDoLaunch, Launch(....))

 

Now, that should solve the first issue of launching even when there is an error.  For the next issue, your SharePoint List has a Title column.  By default, it is required.  You can either change the column to be not required or you can supply a value for the Title column in your EditForm.  That will resolve that issue as well.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

@RandyHayes Ok, I have done all you suggested and just done a test run with Button3 (just submit form), and the more complex Button2 (with the UpdateContext), but I cannot click the buttons (you suggested to disable the view?)  I can see it, but it is grayed out and not clickable.

 

 

MShelnutt
Helper IV
Helper IV

I changed the DisplayMode back to Edit and all is working correctly now.  So other than that suggestion, everything worked.  Thank you!

RandyHayes
Super User III
Super User III

@MShelnutt 

I believe you might have misunderstood my point about disabling the buttons.  The purpose was to utilize a formula on the displayMode properties that validates the form before allowing the user to submit.  This avoids executing the submit on an invalid form and thus getting nasty banner errors in the app.

 

If you have no complex validation for your form, then simply put the following into all three of the DisplayMode properties : If(Form1.Valid, Edit, Disabled)

The built in validation of a form will already validate for required fields.

 

If, however, you have more complex validation (I'll use the example that a specific field in the form must be 4 characters long - let's call it AccountCode), then you can extend this out.

Since you are dealing with 3 buttons that should technically not be "available" until the form is complete, I would suggest (instead of copying and pasting the same formula, and then maintaining it, in three places) placing a Toggle control on the screen (let's call it tglFormValid)

In the Default property of the toggle, set the following formula:

 

 

Form1.Valid &&
(Len(Form1.Updates.AccountCode)=4)

 

Now, for the DisplayMode property of the 3 buttons, set the formula to the following:

   If(tglFormValid.Value, Edit, Disabled)

 

You can set the visibility of the toggle to false so it is not seen by users.

When the condition in the default property ( form is valid AND the length of the AccountCode is 4 characters), then the toggle will change to true/on.  The formula in the DisplayMode on the buttons just looks at that toggle value.

Advantage, your validation formula is only in one place.  If you need to change anything, you only do it in one place.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (20,571)