How to toggle creation/deletion in a workflow

I’m running into a race condition because I’m trying to add an item if it doesn’t exist and remove it if it does. I’ve put conditions on the two actions, but the problem is that they’re not running exactly at the same time, so in some cases it’s creating the item, which makes the condition on the other action true, and it deletes it.

So it’s creating and then instantly deleting the item, or it’s deleting then instantly re-creating it.

If I could create an if/else condition, to force only running one of the conditions, then I could solve this.

For those who want to know the specifics of what I’m doing, here it is, but I’m still interested in the abstract question because even if there’s a better way to do this for this particular use-case, I still want to know in a general sense if there’s a way to force only one of the two conditions to run.

Specifics on what I want to do: I have a list of items. When you click on an item, it adds a new thing to the “bookmarks” table. If you click it again, it will remove that exact thing from the bookmarks table.

Hi there, @brenton.strine… a screenshot of your setup might help, but if I understand your post correctly, you have one workflow event that has two actions in it, and each action has a condition on it, right? If that accurately describes your setup, it doesn’t surprise me that you are running into the issue you described. One way to address the situation is to have two workflow events (each one only having one action in it), and put your conditions at the event level. With that configuration in place, you shouldn’t get into the situation you described because only one of the events will run.

Hope this helps.

Best…
Mike

2 Likes

I think that changing it to 2 separate workflows should fix it. Make 2 separate workflows and add the “only when” onto the workflow instead of on the blocks inside the workflow.

1 Like

I’m really interested in the details of this. What stops Bubble from running the first event, adding the item, and then looking at the condition on the second event, seeing that the item has been added, and then also running the second event?

Additionally, why does that happen if they’re in the same event, but doesn’t happen if they’re in separate events? Are event conditions guaranteed to be evaluated simultaneously?

If you have never come across this post, check it out… but essentially because Bubble says so. :slight_smile:

2 Likes

Hm, from that article:

  • Frontend workflow actions run in order but the next action does not wait on the previous action to be complete before triggering.

If they run “in order”, there’s a chance that one of them will complete before the next stops. That would mean that putting the conditions on the workflow would not guarantee that this problem wouldn’t occur. Am I understanding that wrong?

Also from the post…

The bottom line (for me, at least) is that it works. However, you do have other options for “enforcing” workflows. You could make use of custom events or you could have workflow actions that use the result of a previous action to determine if/when they are run.

Thanks.

I don’t like the wording “safer” because it implies that it’s still not a guarantee.

The last thing you said sounds like it would be guaranteed to work though:

have workflow actions that use the result of a previous action to determine if/when they are run.

I could do it this way:

Step 1: (If item doesn’t exist yet) | Create item
Step 2: (If result of step 1 is blank) | Delete item

In other words, if step 1 doesn’t run because of the condition, then the result of step 1 would be blank, and step 2 can run. This would guarantee that only step 1 or step 2 runs, but never both.

1 Like

I don’t believe I have ever had a problem with placing conditions on workflow events, but yes, you could go the route you just described, too.

This topic was automatically closed after 70 days. New replies are no longer allowed.