Tip: issues using ListAppend: first record duplicate of last; first record missing

Tip: issues using ListAppend: first record duplicate of last; first record missing

  
Symptoms
 
When manipulating record lists using ListAppend, you may find that you are having one of two problems:
 
- In lists of 2 records, you see that the first record of the list is missing; Instead, you have 2 copies of the last record.
 
- In list of more than 2 records, you see that the first record is missing. Instead, the last record of the list is copied to the first record.

 
Cause

TLDR You are using the “List.Current” record to add records to your list as a shortcut and that shortcut has that behavior. Fix your app by creating a temporary record that you use to make all the insertions.
 
Longer explanation
This problem happens when you want to copy records from a list to another list, where the original record is one of the fields. In these patterns, it is tempting to use the “Current” record of the target list as a shortcut - avoiding the need to create a variable used only for the append operation.

The reason this problem is possible (to begin with) is the fact that OutSystems cannot provide an “empty” list - meaning, a list where you would not be able to assign to its current record - another of the consequences of the strongly-typed nature of OutSystems.

This means that, in practical terms:
- An empty list has 0 records (length is 0) but you can still assign to List.Current still (the “current record” of the said list).
- If you don’t explicitly add the record to the list, the list will still be empty, BUT List.Current still shows you the values you have.
- From the moment the list has a record, List.Current still will point to a valid record of the list. If you assign to it, you are altering the list.
 
I created a simple application in my Personal Environment that shows how this happens. I attached it to this post as well so you can look at what’s being done.

 
Resolution

After you’ve read the cause, you’ll find that the problem is wrong use of List.Current in your application. To fix it, stop using List.Current to fill in the record list; instead do it by creating a temporary record that you use to make all the insertions..
 
Hope this helps!