Get Order Metafield Values and Run Calcs to update another Metafield

Hi i want staff to manually track manual installment payments into a custom metafield list of metaobjects each with fields regarding each installment. the metafield is named payment_installment

then when this metafield is updated i have a trigger to run a flow.
in the flow i want to get a total of all the installment amounts and then deduct it from the balance of the order so i can then store the latest balance remaining into a balance_remaining metafield for staff to reference.

is this possible using flow and run code or similar actions without having to use an external endpoint app?

Not sure how to access the amount field within each listed metaobject on the payment_installment metafield to then run the calcs with the order balance and store the new balance amount. any help would be much appreciated. thanks

I would think that you may not need a “Run Code”, but go straight to “Update metafield” and then make your calculations in liquid, inside the new value field.
That’s what I try first.

If not, then you can try to “get metaobject entries” and build a custom query for this by extracting MO ids from the metafield value… Then either “run code” or simply use sum action…

thanks for your help,

I am using the Flow Trigger Extension to listen to the payment_installment metafield so any updates made to the field triggers the flow. this works and i can get the order / order id.

the liquid code you sent doesnt work as

  1. the payment_installment refers to a list of metaobjects called payment_installment and each one has a amount field.

  2. the total amount needs to be deducted from the balance payable of the order.

You need to perform calculations in the Run code action after you received the order data. The Run code action has access to metafield data if you have previously added it to the workflow.

The sequence of actions I see that will help you solve your task:

  • After the Get order data action, add For each action, and then Log output so that the Add variable button becomes available to you

  • Using the Add variable button, select the Metafield field from For each get order data and in the opened window select the tracked metafield. This will add this metafield to the workflow, after which you will be able to select it in the Run code action. After you have added the metafield to the workflow, you can even delete the Log output action, as it was only needed to access the Add variable link

  • Next, you can add the Run code action and select the metafield values in it to calculate the new value you want to set. In the following example I have a metafield with the list_of_metaobjects key:

If you need a ready-made solution, you can contact the Flow Companion app support. We can develop this workflow for you. The app provides the Order metafield changed trigger that tracks changes to the values of selected metafields.