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_idas the entity identifier. - If you prefer to use the stock from the branches that have a higher number of
Stockfirst, you can sort theStockAvailabilityarray by the value of theStockproperty. 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 moststockto the least one. - Since we’re going to get the remaining quantity stored in the storage inside each object of
StockAvailabilityarray, we need to copy theprocess_idto each object of the array usingRecursively copy values to children. - Loop the
StockAvailabilityarray using Node, transform nodes. - Within each object, get the remaining quantity using “Get an entity from a storage” with the copied
process_idand put it as something likeexpected. - Subtract it with the
stockusing Operator Transformer and put the result asremainingQty. - Save the
remainingQtyback to the same storage with the sameprocess_id. - Put a conditional where if
remainingQtyis greater than0, then the usedquantityshould be the same to thestockof the branch. Otherwise, set the usedquantityas same as theexpected. - There must be a condition where the total quantity has been fulfilled, even though we still have branches whose
stockis not even used. Due to the subtraction and the conditional done previously, thequantitywill be a negative number. We can put a conditional or Value mapper to change it to0if 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.