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.

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.