Hi @abubakar
In order to achieve the objective, we need to be able to set the remaining quantity variable whenever we have finished setting the quantity needed from each branch in a loop. Unfortunately, Alumio cannot get any properties and their values from outside of each branch object without the “Recursively copy values to children”. Furthermore, we also cannot modify any of them. The only way I can think of is to store the remaining quantity variable inside storage. Something that we can easily read and write, no matter the position in the entity.
Please find the below steps we could do to achieve the objective.
- Create a unique storage identifier to prevent any collision between calculations. I would recommend using the current timestamp (including microseconds). You can refer to How to map with Format: Date. You can combine it with any unique value within the entity. We could name the property as
process_id
. - Store the remaining quantity to a storage using the value of the
process_id
as the entity identifier. - If you prefer to use the stock from the branches that have a higher number of
Stock
first, you can sort theStockAvailability
array by the value of theStock
property. You can use the JMESPath built-in functions sort_by and reverse to sort the array by specific property descendingly. This way, the loop will start from the branch that have the moststock
to the least one. - Since we’re going to get the remaining quantity stored in the storage inside each object of
StockAvailability
array, we need to copy theprocess_id
to each object of the array usingRecursively copy values to children
. - Loop the
StockAvailability
array using Node, transform nodes. - Within each object, get the remaining quantity using “Get an entity from a storage” with the copied
process_id
and put it as something likeexpected
. - Subtract it with the
stock
using Operator Transformer and put the result asremainingQty
. - Save the
remainingQty
back to the same storage with the sameprocess_id
. - Put a conditional where if
remainingQty
is greater than0
, then the usedquantity
should be the same to thestock
of the branch. Otherwise, set the usedquantity
as same as theexpected
. - There must be a condition where the total quantity has been fulfilled, even though we still have branches whose
stock
is not even used. Due to the subtraction and the conditional done previously, thequantity
will be a negative number. We can put a conditional or Value mapper to change it to0
if the number is negative. - Delete the storage entity used for the calculation using “Delete an entity from a storage” to prevent the increment of the number of storage entities.
The above steps are quite long but can actually be used to spread the stock usage between the branches. I hope the explanation is clear to you.
Please let us know if you have any questions.