Appian a!localVariables()
Name a value so you can use it more than once, and reach a value from an outer loop. The last argument is the answer, which is the part people miss.
Official Appian documentationThe shape
a!localVariables() lets you name a value, then use that name. Everything before the last argument is a named local; the last argument is what the whole thing returns.
a!localVariables( local!x: 10, local!x * 2 ← this is the answer )
That returns 20. The locals are scaffolding; the final expression is the result.
The last argument is the answer, not the last local
This is the first thing people get wrong. Naming three locals and stopping does not return them — it is an error, because there is no final expression:
a!localVariables(local!x: 1) ✗ missing body expression a!localVariables(local!x: 1, local!x) ✓ returns 1
And when there is a final expression, it is the one that decides. a!localVariables(local!x: 1, local!y: 2, local!x) returns 1, not 2.
Locals are read in order, and a forward reference is silently null
Each local can use the ones defined above it. That is what makes chains work:
a!localVariables( local!x: 10, local!y: local!x * 2, local!x + local!y ) → 30
Reaching downwards for a local that has not been defined yet does not fail. It quietly evaluates to null, and the wrong answer travels on from there. Order matters, and nothing will warn you when you get it wrong.
Why it is worth using
Three reasons, in the order you will meet them:
- Compute once, use twice. Writing
count(items)in three places asks the engine for it three times and asks the reader to notice they are the same thing. - Name the steps. A long expression becomes readable when its parts have names.
- Reach a value from an outer loop.
Reaching out of a nested loop
Inside a!forEach(), a second a!forEach() gets its own fv!item and hides the outer one. There is no fv!parent. Catching the outer item in a local first is the way through:
a!forEach(
items: rows,
expression: a!localVariables(
local!row: fv!item,
a!forEach(items: cols, expression: local!row * fv!item)
)
)The local is defined in the outer pass, so the inner expression can still see it after fv!item has been taken over.
Try it
Run each one and read what comes back before moving on.
Name a value, then use it.
a!localVariables(local!x: 10, local!x * 2)
1Define two locals and return the first one. Read the result carefully.
a!localVariables(local!x: 1, local!y: 2, local!x)
2Now leave the final expression off entirely.
a!localVariables(local!x: 1)
1Chain one local off another.
a!localVariables(local!x: 10, local!y: local!x * 2, local!x + local!y)
2Now reference a local before it is defined.
a!localVariables(local!y: local!x, local!x: 1, local!y)
Use a local to reach the outer item from inside a nested loop.
a!forEach(items: {1, 2}, expression: a!localVariables(local!row: fv!item, a!forEach(items: {10, 20}, expression: local!row * fv!item)))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- 1a!localVariables(): summarise a list as sum over countEasy
- 2a!localVariables(): show the price range of a productMedium
- 3a!localVariables(): compare every score to the averageMedium
- 4a!localVariables(): label every seat in a venueMedium
- 5a!localVariables(): write a basket summary with free deliveryHard