Updating a thing only works some of the time

Hi everyone. I’ve been struggling on the same (seemingly small) problem for over a day without much luck, so hoping the community can help!

I am building an app where users need to set 6 percentages that add up to 100%. I’ve added functionality so that if the 6 percentages add up to more than 100%, the most recent value is taken as-is (assuming it’s not more than 100%, in which case it’s automatically set to 100%), and then all the other 5 values are adjusted down proportionally. For example, if the splits are 50% and 5 x 10%, moving 50% up to 60% will automatically result in the remaining 5 splits being set to 8%. You can see the functionality in the linked app in Customer Splits fields under Market Data.

This functionality is all working well, but I’m trying to write these values back to the thing in the database to store them in real-time. I’ve decided not to use auto-binding - perhaps this was a mistake and I’m open to doing it differently, although I’d ideally like to work the solution through without auto-binding. I’m using events after an element is updated to trigger the logic, update states, and then update the thing.

The problem I’m running into is that it is often updating the thing with old values. Let’s say I’m updating the first % (Segment 1), the behaviour I have observed is:

  • When typing directly in Segment 1 the thing is updated with all of the correct values, but not on the first time after a page refresh - the 2nd (and onwards) time you update Segment 1, the thing will show the correct values
  • When updating Segment 1 by clicking a button (e.g. the corresponding +5%), the the field in the table corresponding to Segment 1 will update each time, but the other fields (Segment 2-6) will update with the value that it used to be before the most recent update - so always one step behind

I’m confused at this behaviour because I wouldn’t have thought that the button would have a different behaviour than typing directly into the field, especially because they both feed into the same custom workflow. When I go into the debugger I’m barely able to progress past the first step before the thing is updated, and then by the time it gets to the actual step where the thing is supposed to be updated nothing happens. This partially explains to me why it’s one step behind - the thing is being updated with old values, but only when you press the button, and then there is the separate issue of the first time vs. the 2nd+ time. I’m sure these are significant clues but I just can’t work it out…

Lastly, I haven’t built out all of the functionality because of this issue. In the app linked below, please change Seg1 - either directly by typing into the input box, or by clicking +5%. If you need to reset the values (as it will update the thing for everyone), just add 50% for Segment 1 and then 5 x 10% for Segments 2-6. If you finish that sequence by typing 50% into Segment 1, it should all work, ready for a page refresh.

Link to the app - Test-20220701 | Bubble Editor

Thanks in advance - I’ll be massively grateful to anyone that can point me in the right direction!