All functions

Appian index()

Read an item by position or a field by name, with a value to fall back on. Given a list of records it returns that field from every one of them, which is the trick most people miss.

Official Appian documentation

The shape

index() reads something out of something else, and takes a value to fall back on when it is not there.

index(from, what, default)

The interesting part is that what can be a position or a field name, and from can be a list, a record, or a list of records. One function, four jobs.

Reading by position

Positions count from 1, like everything else in Appian except enumerate().

index({"a", "b", "c"}, 2, "")   →   "b"

The default is the whole point

Ask for a position that is not there and you get the default back, not a failure:

  • past the end — default
  • position 0 or a negative — default
  • an empty list — default
  • a null in place of the list — default

This is what makes index() safe on data you did not produce. Leaving the third argument off is the usual mistake: it works on every input you tried by hand, and fails on the one that arrives from a user.

Reading a field out of a record

Give it a field name instead of a number and it reads that field. A missing field takes the default, so a record with gaps does not bring anything down:

index(a!map(name: "Ann", city: "Pune"), "name", "")   →   "Ann"
index(a!map(name: "Ann"), "city", "unknown")          →   "unknown"

The one worth knowing: a field from every record

Hand it a list of records and a field name, and it returns that field from all of them, in order:

index({a!map(n: "A"), a!map(n: "B")}, "n", "")   →   {"A", "B"}

No loop. This is the shortest way to turn a list of records into a list of one column, and it is the thing people write a!forEach() for when they do not know it exists. Records missing the field take the default in their own position, so the result always lines up with the input.

Several positions at once

The second argument can also be a list of positions, which returns a list of values:

index({"a", "b", "c"}, {1, 3}, "")   →   {"a", "c"}

Each position falls back independently, so one bad position in the list costs you one default rather than the whole result.

Try it

Run each one and read what comes back before moving on.

1Read the second item of a list.

index({"a", "b", "c"}, 2, "")

2Now ask for a position that does not exist.

index({"a", "b", "c"}, 9, "none")

3Try it on a list with nothing in it at all.

index({}, 1, "none")

1Give it a field name instead of a number.

index(a!map(name: "Ann", city: "Pune"), "name", "")

2Now hand it a whole list of records and one field name.

index({a!map(n: "A", c: "P"), a!map(n: "B", c: "D")}, "n", "")

Ask for more than one position.

index({"a", "b", "c"}, {1, 3}, "")

Independent evaluator — approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms

Exercises

0 of 5 cleared
  1. 1index(): read one item safely by position
  2. 2index(): read a field out of a record
  3. 3index(): pull one field from every record
  4. 4index(): turn survey answers into labels
  5. 5index(): summarise every shipment on a tracking page
Start the first exercise