Skip to main content
OutSystems

How to control what variables a list have when assigning in a loop

Question

How do I make sure that a list of records created in a loop has independent records, and not references to the same record?

For example, I want to:

  • Run a monthly loop over a set of days
  • Accumulate monthly totals in a Record.
  • Add monthly totals to a list.

How do I make sure that each monthly total is a "new object" and not just a reference to the same loop variable?

Answer

When you assign or pass variables:

  • Records are always copied and not referenced.
  • Lists are always referenced. Use ListDuplicate to duplicate a list.

In particular, adding a record to the list stores a copy of the record in the list.

Therefore:

  • Use a single Result Record to accumulate your monthly values.
  • Add the Result Record to the list to store a copy.
  • Clear the loop record for the next month.

 

 

You can clean your Result Record attribute-by-attribute, as shown below.

unnamed.png

Another option is to have another record of the same type of your Result. Use the second record to clean the entire Result Record. This option is useful for Entities with lots of attributes.

In the example below, the auxiliary record Order_AUX is used to clean the record Order.

unnamed (1).png