cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
flyte
Helper II
Helper II

Prevent Duplicate Inserts During a Patch in Button's OnSelect

I have a "Submit" button performing a Patch to do a record insert to a SharePoint collection. AutoDisableOnSelect is set to true which will disable the button while the OnSelect action is being performed. 

 

I am finding that during the "lag" of which the Patch is trying to insert the record into the list, I am able to press the button again, and thus a duplicate entry is inserted. This seems to be happening often with my users because there isn't any visual feedback that the Patch is in progress, (like a progress bar or something) and the users seem to want to hit the Submit button again.

 

Any suggestions on how best to solve this?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Thanks @Meneghino and @v-micsh-msft for the suggestions. I simply set the submit button's visibility to false when the patch starts so there is no chance that it can get hit once again. It seems to have solved the problem with the easiest code change.

 

 

View solution in original post

10 REPLIES 10
v-micsh-msft
Community Support
Community Support

Hi @flyte,

 

We could wirte formula to first check if the data exists in data source, if exists, do nothing, and if not, update.

Would you please share some more information regarding patch function you used here?

The onselect property should be in the format below:

If(IsEmpty(Filter(datasource, Value in column)),           // This part checks if the valueexists

   patch(datasource, baseRecords, updateRecords)    // If IsEmpty returns true, which means there is no record created before

)                                                                                    // If IsEmpty returns false, do nothing

Adding some reference:

understand data sources in PowerApps

Filter, Search, and LookUp functions in PowerApps

Hope this information is helpful.

Regards

Community Support Team _ Michael Shao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Meneghino
Community Champion
Community Champion

hi @flyte

 

in addition to what @v-micsh-msft is suggesting, you could try to do this:

MyButton.OnSelect = UpdateContext({Patching: true});
Patch( whatever you patch );
UpdateContext({Patching: false})

and

MyButton.Disabled = Patching

Not sure if it will work, since this is what should be happening with the AutoDisableOnSelect anyway.

 

If this does not work, it is possible that the patch operation is quite quick and the delay is in PowerApps updating the data source information after the patch and refreshing all screens where this data source is used.  The more screens and formulas where you use the datasource directly (i.e. in galleries) then the longer the time to refresh.  You could review and optimise the use of direct reference to the data source.

Thanks @Meneghino and @v-micsh-msft for the suggestions. I simply set the submit button's visibility to false when the patch starts so there is no chance that it can get hit once again. It seems to have solved the problem with the easiest code change.

 

 

View solution in original post

 @flyte,

 

Thanks for the update and sharing here.

If you need any further assistance regarding PowerApps usage, please post back.

Regards

Michael

 

Community Support Team _ Michael Shao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@flyte, Your visibility solution is good. Another possibility to consider is to cover the screen or button with an animated GIF. This is like @Meneghino's solution.

 

Setup Button.OnSelect to:

 

UpdateContext({wait: true});

Patch(your patch code goes here);

UpdateContext({wait: false})

Set Image.Visible to

wait

 

You can set the Button.Visible to the opposite if you want it hidden when the GIF is showing too:

!wait

 

Microsoft Employee
@8bitclassroom

Thanks for this solution. How would I prevent the image loading gif come up for all the buttons in a vertical gallery? I would want it to only apply to the button that was clicked.

mr-dang
Community Champion
Community Champion

@laurel_cpi wrote:

Thanks for this solution. How would I prevent the image loading gif come up for all the buttons in a vertical gallery? I would want it to only apply to the button that was clicked.


First you'll need to identify which record is affected. In this solution, I am assuming that 1) you are not Patching new records but editing existing records that already preside in your datasource, and 2) your Gallery is mapped to a connected datasource and not to a temporary Collection. The formula below activates the saving sequence with "wait" and identifies the chosen record:

 

 

UpdateContext({temprecord: ThisItem, wait: true});

Patch(); UpdateContext({wait: false})

 

 

Use a column from the record that has unique values to identify it as the record which you want to be affected by the loading GIF. If you are using CDS, you can use PrimaryId or RecordId since they are autogenerated and unique. The record you set into the temprecord variable can be recalled using:

 

 

temprecord.RecordId

 

Now you can set the visibility properties of the Button to only be Visible if it is not the Patched record and if your wait variable is off. This means, "Show the Button in all situations except when "wait" is on and the RecordId matches the one you've patched." The GIF is only visible when the Button is not. ! means "Not"

 

Button.Visible:
!(wait && RecordId=temprecord.RecordId)

Image.Visible:
!Button.Visible

If you have Button inside a Gallery that is mapped to a Collection that is not a connected datasource, and you want it to have the same visibility behavior, it will work the same way. You will just need to identify something unique about each record so that only one record has its button hidden when Patching.

Microsoft Employee
@8bitclassroom

Perfect! Thank you for the quick and detailed response. This is just what I was looking for.

Hi Meneghino and PowerApps community

 

I am new to PowerApps, but I managed to make an application what works exactly like Siteinspector, it collects data to an excel sheet and photos what placed to dropbox. There are more than 10 users at the moment who are using the app and it turned to be an issue while multiple users submit a form on their mobile device.

 

When 2 or more users submit their form in the same time, I get 2 records with the same ID (sometimes ID`s are OK), where the the 1st user`s photos linked to the 2nd user`s record. The text inputs for each records are seems to be correct in both records in most cases, however If I delete the photo or record of the 2nd user and try to create a new record/ photo, 1 st user loose all his pictures too.

I have made a waiting screen with a smal gif animation and set autodisableonselect true on save button but could not solve the issue.

 

  • Is it possible to show an error message or disable save button on user 2 device if user 1 already submited the form until the saving is done?
  • Is it possible to check before submitting the form if ID & Title & Subtitle already has a value on that ID what it want to update and if it returns true stop creating the new record and gives a message "try again in 30 seconds"

Somehow i need to find a way to prevent records being corrupted by another user.

 

Please help to find a solution to this issue, my users are using the app so offen that I cannot avoid situation like this.

 

Thank you very much in advance

All ideas are appreciated 🙂

 

BR.Tamas 

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

Top Solution Authors
Top Kudoed Authors
Users online (68,648)