How to correctly set up a workflow with a Loop and Calculation of the sum from the Loop?

Hello!

I have a collection of Invoices and Invoice Services. When submit a New Invoice form, I need to create associative (subordinate) records of Invoice Services, and then perform a summation (Total) on the numeric field in the created Invoice Services “back” to the Invoice.

I created a workflow with nodes of approximately the following structure:

  1. Trigger (fires when the New Record form of the Invoices collection is submitted). That is, when a new Invoice record is created.
  2. Loop - “inside” the created Invoice, new Invoice Services records are created. There is a numeric field called Sum, which will need to be used in calculating the Total in the Invoice itself.
  3. Query Record - newly created records from the Invoice Services collection (those created using the Loop) are selected. But here I cannot refer to the previous Loop Node, as there is no such function in the settings. I create a condition for filtering records where the ID of the associated Invoice = ID of the Trigger (i.e., the invoice being created).
  4. Calculation - here you can select the previous Query Record Node, where I use the SUM() function to build an expression that sums the values in the numeric field of the Invoice Services collection.
  5. Update record - I update the Invoices collection record, where I set ID = Trigger ID in the filter condition. And the value from the previous Calculation Node is transferred to the resulting Total field.

Overall, everything works, the workflow is executed, and the result is positive. But I have a question: have I configured everything correctly, and is this workflow optimal in terms of performance?

I also have a question: why can’t I use the parameters of the Loop Node (when new records are created) to then interact with them directly and use them in the Calculation Node? That is, so as not to use an “extra” Query Record Node.

I would appreciate any tips from experienced NocoBase developers!