Spreading the subtracting of number over multiple items

Hey all I’ve come in to a bit of an issue.

I am trying to subtract an amount over a few items.
The use case is that I have multiple boxs with apples in them, when the customer wants to buy 100 apples I want to empty the first box, then if needed empty the second box and take the remained from the 3rd box

ie
Boxes of apples - 1
apples = 30

Boxes of apples - 2
apples = 35

Boxes of apples - 3
apples = 50

Apples required = 100

result

Box of apples 1 = 0 and is deleted
box of apples 2 = 0 and is deleted
box of apples 3 now has 15 apples

I was trying to get away with a sneaky api loop where 2 identical API’s referenced each other in a loop but it appears @emmanuel Bubble Bot police where smart enough to see me coming and the indirect self reference have flagged as errors. I havent tried to see if it works as I presume it needs to be corrected before going live anyway.

I was going to try doing an API on a list however i’m afraid i will most likely encounter a run condition and everything will try and fire at once. Either that or i just cant seem to get my head around the equation needed to complete the task.

Hi @chad,

Do we assume you want this done purely on the server side, i.e. with API workflows?

Which amounts are variable and limited, i.e. can change each time it is invoked, do you know upper/lower bounds? For amount of apples, number of boxes, box size.

If you have a small number of boxes, one approach is to have a custom workflow, called for each box, in which you compare the box content with a stored remaining amount, to decide on subtracting partial or whole.

Server side is ideal however not necessary only due to UX speed, I can have it happen without the use waiting.

Both amounts are variable. There could be any amount of apples in the box, the boxs are not necessarily set and the user may require any amount of apples.

There also is boxes full of oranges, apples, pears, necatarines and potential unlimited amount of other fruits.

bummping

So you could run an update on all the Boxes, summing up the Boxes where the Box number <= the current one …

Boxes of apples - 1
apples = 30
Sum = 30

Boxes of apples - 2
apples = 35
Sum = 65 (30 + 35)

Boxes of apples - 3
apples = 50
Sum = 115

Apples required (x) = 100

Now run another set of updates…

If Sum < x (100 in your case) then update Apples to 0
If Sum > x then update Apple to Sum - x (so 15)

so your suggesting having a field called sum that contains the total of all the boxes

how would i then go about deleting the empty boxes?

You could delete all the boxes where sum < x

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