Best Practices for calculating totals

I’ve run into this problem a few times. I’m generating a shopping cart and run some backend workflows to create the amount for a number of line items. I’d like to additionally calculate the subtotals and totals, however, the problem is that steps in workflows don’t run sequentially. So I can’t calculate the total because there’s no way to know if the line items have been properly filled out yet.

This is a backend workflow so there’s no “pause” action available.

I can do the subtotals fairly easily on the frontend, but then adding all the subtotals together is a huge pain because I can’t grab the values out of the text fields, I have to actually re-build the math for each subtotal into a massive calculation for the final total.

This is a problem because it’s incredibly difficult to edit these massive tallys, and even a small change could mean rewriting the entire thing, and that increases the chance of errors being introduced.

Is there a better way to accomplish subtotals and totals?

The plugin name is WAIT. I fixed a lot with it.

1 Like

I use database triggers, then when I calculate a subtotal from the trigger, then the next step is to calculate the tax I change the “Result of step 1” so it waits for the 1st step to complete

1 Like

Question: Does scheduling another backend workflow from within a backend workflow happen synchronously or asynchronously?

From what I can tell, that plugin doesn’t guarantee synchronous execution, so if you tell it to wait for 1 second but one of the tasks takes 2 seconds to complete, things may not happen in the right order. I’m eager to be corrected if I’m wrong on this.

Do you have a recursive workflow that creates these items? Can’t you have it so when it completes the recursive workflow then it calculates the totals all at the end?

I certainly can have a recursive workflow.

And I can have it calculate the totals after the workflow completes.

My question is, will that actually force it to be synchronous?

Example:

Backend Workflow 1:
Step 1 (update value) → Step 2 (update value) → Step 3 (update value) → Step 4 (update value) → Step 5 → (Schedule Backend Workflow 2 for .1 second from now)

Backend Workflow 2:
Tally values from previous workflow.

My question is, if it takes Workflow 1 ten seconds to complete, will Workflow 2 wait until Workflow 1 is completed to start? Or will it just start in 0.1 seconds?

It’s safe to assume it’s going to fire whenever Bubble’s algorithm says there is time to fire it so assume asynchronous just to be safe. (However it doesn’t hurt to just try it and stress test it with 100 items and see if it comes out perfect)

I don’t know much about your app, the changes you’re making in workflow 1, are you basically sending a list of products to your workflow and generating line items for each? Can you send some screenshots?

What I picture is you send a list of products to your workflow, then it creates a line item for the first one, reschedules itself:minus first item, processes the next one, reschedules itself again, etc until it hits 0. Then you have a conditional on the last step so when the list is 0 you schedule the total calculation?

I’m trying to come up with a general solution to use in any and all future apps that I develop. I’ve run into this same exact problem multiple times where I’m making a ton of changes to values all at once, and then I want to do some math using all those values ONLY WHEN I’m sure all of the values have been completely changed.

I guess a more general way to put this is, is there a way to FORCE workflows to happen synchronously?

Could do it that way if it would help, but I still am not clear on if doing that actually forces it to happen synchronously, or if it just hopes that the tasks take less time to complete than the delay you set for scheduling the next.

I don’t like using the word “hope” when developing mission-critical business logic. That’s really what all this comes down to.

Just a side note I’ve been eyeballing this plugin made by Keith and this might help out. List Popper and Friends (SSAs) Plugin | Bubble

So lets say your task is to send a list of products to the backend, and you want to create a list of shopping cart items for each product sent over. and lets pretend that your new Shopping Cart Item will have a field called Product referring to the original product.

  • Step 1 in your backend workflow would be “Create a thing” Shopping Cart Item, and you set the Product field to the list of products:first item.
  • You reschedule this workflow but the list you send is list of products:minus item Result of step 1's Product.
  • So Bubble will 100% wait to reschedule itself because it needs the result of step 1 to determine that list.
  • Put a conditional on that reschedule so it only runs when list of products:minus item Result of step 1's Product:count > 0.
  • Your next action would be triggering the next workflow 2, that calculates all the totals. But you have a conditional list of products:minus item Result of step 1’s Product:count is 0 So everytime this workflow loops that conditonal fails, until the very last loop it waits for the step 1 to complete, then triggers the total calculation.

This is kind of a brain dump of what I remember so hopefully it makes sense

EDIT: formatting so it’s not an eyesore :joy:

1 Like

@brenton.strine

It forced (like Custom Event) to execute it first and then continue. It’s stopped parallel processing for a moment waiting for a result.

You have to use “Only when” done waiting:

image

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