Skip to main content
Question Most Common

Given an array with null values, how would you return a new array without any nulls in Appian?

Answer

Use the reject function to filter out all null values from the array.

Code:

a!localVariables(
local!data: { 1, null, 6, 8, null, 19, 30, null },
reject(fn!isnull, local!data)
)

Output:

1762361173723-8jwxnqthqu8.png













Explanation:

a!localVariables(...) creates a scope for local variables. Variables defined inside are available only within that scope, and the last argument is the value the expression returns.

  1. local!data: { 1, null, 6, 8, null, 19, 30, null } defines a list of eight items, three of them null.
  2. reject(fn!isnull, local!data) takes a predicate function and a list, runs the function on each item, and drops every item for which it returns true. With isnull() as the predicate, every null item is removed.


Replacing null values with Zero in Appian:

Sometimes Interviewer may ask a follow up replace the "null" with default value, in your case let replace the "null" with default value - zero


Code:

a!localVariables(
local!data: { 1, null, 6, 8, null, 19, 30, null },
a!forEach(
local!data,
if(a!isNullOrEmpty(fv!item), 0, fv!item)
)
)

Output:

Image 125








Explanation:


a!forEach(local!data, if(a!isNullOrEmpty(fv!item), 0, fv!item)) loops over a list, runs an expression on each item, and returns a new list of the results.


What happens in the loop

  1. a!forEach goes through the items in local!data one at a time, in order.
  2. On each pass, fv!item holds the current item.
  3. if(a!isNullOrEmpty(fv!item), 0, fv!item) returns 0 when the item is null or empty, and the item itself otherwise.
  4. When the last item is done, a!forEach returns all the results as a new list.


Step by step with { 1, null, 6, 8, null, 19, 30, null }


Pass

fv!item

Null or empty?

Returned

1

1

No

1

2

null

Yes

0

3

6

No

6

4

8

No

8

5

null

Yes

0

6

19

No

19

7

30

No

30

8

null

Yes

0

Result

{ 1, 0, 6, 8, 0, 19, 30, 0 }

The result has the same length and order as the original, with each null replaced by 0. local!data itself doesn’t change.

Try it yourself

Change the inputs or the expression and run it to see what it returns.

ri!data=

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

JuniorExpressions#coding