Collections in validation script

A collection is a list of data. Usually you create it when retrieving attributes or relations.

You can refer to a specific element in the collection in multiple ways.

Note If a reference points to a value that doesn't exist, the rule will crash. For example, retrieving the fifth value of a list that only contains four values causes a crash.

Reference Result
mylist.first() The first value in the list mylist.
mylist.last() The last value from the list mylist.
mylist[1] The second element from the list mylist. The number is the index of the value you want to retrieve from the list.

Note The first element has index zero. As a consequence, mylist.first() is equivalent to mylist.[0].

mylist.get(1) The second element from the list my list. It is very similar to the example above.
mylist?.get(1) The second element from the list. However, this syntax is null-safe. ?. is the null-safe getter of Groovy. You cannot combine the ?. with the [ ... ] notation. This means that if the referenced attribute is not present, the result is null, whereas the expression without the question mark fails to execute.