Backend workflow running immediately (and not when it should - at end of workflow)

HI gang. I am trying to understand some bizarre, head-scratching behavior, hoping someone can help…

I have a 5-step workflow where when a user clicks ADD, the first thing I do is search the database to see if a particular thing exists. If it does not, i.e. the thing from step 1 is empty, I do steps 2-4. Step 5 is I “Schedule an API Workflow” where I go and create that thing I was looking for in step 1. This is so that NEXT time a user tries to add the same thing, step 1’s thing is NOT empty and steps 2-5 get skipped as they’re unnecessary.

Here’s the weird part. Using the debugger step-by-step, when I click Add , and I know definitely that the step 1 query should come back empty, I’m intermittently seeing that thing appear in my database the moment I step into my workflow. It’s like step 5 - the backend workflow which creates the thing - is running as step 0, before step 1. Is this possible?

Does a workflow action step that Schedules an API Workflow not honor the sequence/order of operation of the workflow? I would expect it to not run until steps 1-4 have executed (or been skipped as the case may be).

The only thing I can think of is there’s a slight chance these records I’m seeing appear in my Db prematurely ONCE existed in my table, but were subsequently deleted in my early days of development. If I deleted records through the Bubble Editor, is it possible the records weren’t actually deleted but simply hidden? And my step 1 query is finding them, despite having been previously deleted?

Hi there, @ThatFizzGuy… yup, it’s possible (and even probable), and you should definitely read what is, in my opinion, one of the most useful posts to ever be posted in the forum.

Best…
Mike

3 Likes

“Backend workflows are triggered as soon as the workflow is triggered, independently from steps. For example, a ‘Schedule API Workflow’ action will be triggered as soon as the workflow is triggered even if it is placed last in the workflow action sequence.”

@mikeloc you are a godsend. You may have just spared me from the men in white coats. I thought I was going mad.

1 Like

useful post is right. That post should be page 1 in the manual! Great stuff, thank you again!

1 Like

FYI They did eventually add this to the manual too so that you can easily reference it instead of tracking down blog posts :joy:

https://manual.bubble.io/help-guides/building-workflows/the-basics#execution-rules

2 Likes

Yeah, I know it’s in the manual, but I have the blog post bookmarked, so it’s easy for me to link to it.

1 Like

Thanks, @bubblealex - bookmarked!

@mikeloc or @bubblealex - quick follow-up: My previously-mentioned, 5-step workflow is part of a Custom Event living in my site header (a reusable element, in case it matters). To experiment with this new learning (your helpful links)… I took my step 5, the Schedule API Workflow step, and moved it into its own Custom Event. Now my Step 5 calls that new custom event. Based on what I read above, specifically this:

“If a backend workflow should be triggered after other steps in the workflow, then it should be implemented in a custom event placed after the steps that need to come first.”

I expected my database record to NOT get created until the very end. But it still seems to be firing immediately. Am I misunderstanding the above recommendation? Is there something else I need to do before that thing creation can happen last? Thanks!

I don’t think you are misunderstanding, but I would probably try a couple of other things. Are you able to put an Only when condition on the API workflow step that says it should run only when the result of step 1 is empty? Or can you separate the workflow into multiple workflow events that have Only when conditions at the event level instead of having all of the actions in one workflow and having conditions at the action level?

thanks @mikeloc . I ended up re-working several things and I’ve achieved my desired order of operations. That said, backend workflows and controlling WHEN they fire are still an elusive beast. I’ve thus far had zero luck controlling when they do their thing. But the urgency has passed so I’ll circle back to better understanding them at a later date. Thanks again for the assist!

1 Like