Can't understand why a backend workflow isn't working

I am trying to use some backend workflows so I don’t need to put them in the main app’s workflows because they will be constantly going on in the background. Mostly number calculations.

I have one set up to calculate the weight of cargo on a manifest.

One of them is set up to change the field
Thing to change: Flight Now
Cargo Weight = Flight Now’s Cargo Weight:sum (as there are multiple cargo items)

  • Only When Cargo Count is not 0

Then another one set up
Thing to change: Flight Now
Cargo Weight = 0

  • Only When Cargo Count is 0

I want the weight to always be set to 0 if there are no cargo items added to the flight to avoid any kind of weird issues in the future.

For some reason, this is not setting the field to 0. Why? The one to add up the sum seems to work fine and it’s using the same logic basically.

If I get rid of the “zero” workflow, the sum workflow doesn’t change the field to 0 which you might think it would if there are no cargo items on the flight.

Any idea why this isn’t working?

Edit: Actually, the Sum operation isn’t working properly either. It works when I add cargo to the manifest, but doesn’t run when I remove it. What the heck?

Please share a screen of trigger of what is calling the backend workflow (on the workflow screen), along with a screen of the actual backend workflow.

image
image

This seems to work but only when cargo are added, but not removed.

For trying to set it to zero - Does nothing
image
image

I’ve since removed the workflows because I couldn’t figure out why they aren’t working, and worked them into my cargo movement workflows instead but that’s really annoying. I have other backend workflows that seem to work fine, like the ones that calculate the total load of the aircraft, setting a title field which is a makeup of several DB fields, and generating flight numbers.

Ah ok. So these do not appear to be true “Backend Workflows” (which are API’s) but rather in-page “custom workflows” correct? If so, are these “triggered” or “scheduled”?

“Triggered” tends to invite a lot of Bubble issues…

They’re DB trigger backend workflows.

The entire point is for the workflow to run when it detects the change in the specific DB items, no?

It works for the other ones. I’m not using any external APIs whatsoever.

This is considerably risky, and doesn’t always work, primarily due to the way Bubble caches DB information to improve its performance. You should tie your custom workflows to trigger/schedule on the user’s click and not rely on data-level changes to trigger them.

I can understand further if you can show the point in the workflow page where it actually is being called to run and the structure surrounding that.

Well the reason I want it to take place in the DB is because there are multiple things that will affect a number.

This one seems to work fine. For example, I don’t want to have to manually plug this calculation workflow in EVERY time I modify any of these fields because it’s a waste of my time. If it can just happen in the background, that is ideal. Any time any of these fields change, this number gets updated.
image

This calculation adds up all the various weights for the flight and spits out the sum, then I have another one that takes the maximum allowed weight of the aircraft and subtracts all these numbers to provide how much weight is remaining/allowed.

For whatever reason, these ones seem to work fine but I can’t get it to calculate a single field in the DB. Makes no sense.

As for what you’re requesting. I added the workflow to calculate the Flight Weight within one of the workflows I have for moving cargo around. Basically how this workflow works is when a user transfers cargo from the warehouse to the flight, it checks to make sure that cargo doesn’t already exist on the flight. If it does, it’s a different workflow for organization reason.
This workflow is to split the cargo, so for example say there’s 5 boxes each 20 lbs but the user only wants to add 2 of them to the flight. So, it checks to make sure that cargo (which has an associated EntryID) does not exist on the flight. If it doesn’t, it creates a new Cargo entry, sets its location to the Flight (Cargo field), also adds the Cargo to the List of Cargo on the Flight (Flight field) along with a couple other things, including deleting the original cargo if the quantity reaches 0.

As you can see, adding the extra step to calculate the flight weight in every workflow is annoying, thus I want it to take place in the background.

@tonymoulatsiotis you are using the :count operator in the trigger - so if it has the same amount of cargo items it won’t recalculate the sum I don’t think?

If a cargo’s weight is set to 0 by default you might be able to then just do the one workflow to update the sum instead of having the conditionals?

What happens if you just have the Flight’s Now’s Cargo is not Flight’s Before Cargo on the trigger?

Well, that’s kind of the point. I want it to recalculate the weight every time the count changes…since that’s basically when I need it to update. I added fields on the page to show me what the count is along with the current “Flight Weight” field, and when I add cargo to the flight, the count changes as expected. The Flight Weight updates with the sum as cargo is added to the flight, but not when it’s removed, nor when it’s zero…which is what I can’t figure out.

I can’t do that. Flight Now’s Cargo is a list of Cargo (DB) items. It doesn’t let me compare them like that. It only gives me Contains /Doesn’t Contain or Is Empty / Is Not Empty along with all the other stuff, but not a straight across comparison, since it’s a list of DB items.

Have you got an option for ‘this flight’ rather than ‘flight now’ - for the thing to change

I don’t have it there, but have it in the field I want to update. I tried both using Flight Now and This Flight there for updating the weight and neither worked properly.
image
image

Like if Bubble is going to implement “features” like this, I would expect them to work as they explain in the documentation which doesn’t seem to be the case.

"Also known as a “database trigger event”, this event is defined in the Backend Workflow section and runs workflows triggered by changes in the database.

Whenever an item of the given type is modified, we check the “Only when…” condition of the event, and if the condition passes, we run the workflow."

So, I guess it’s possible I am not setting the “Only when” correctly, however I fail to see how that could be the case when I have a text field on the page showing the Flight’s Cargo Count and see it changing on the page but the Weight is not being updated.

I also tried setting it to a finer piece of data like the Cargo’s Quantity: sum which would be the sum of all the Cargo entries’ quantities. So, like I said a single Cargo entry could have a quantity of 5. So the Quantity Sum would be 5 whereas the Cargo Count would be 1 if that makes sense.

I also tried setting it Flight Now’s Modified Date > Flight Before Change’s Modified Date which would, if I’m understanding it correctly, mean that every time the Flight data is modified, the workflow would run since Flight Now’s Modified Date should always be greater than Flight Before Change’s Modified Date, right? That is, if it actually checks the seconds… Anyway, that didn’t work either.

Edit: thanks for the suggestions however I’ll have to look into it later. It’s bed time!

I have lots of database triggers going on in my backend workflows and they all work as expected so I’d say there would be something going on with your conditions.

That being said I’m not doing any calculations so not sure if there is a problem there.

I’d email bubble support though, they are really helpful :slight_smile: