Skip to main content
June 25, 2025
Question

Strange behavior in a!forEach, null - {}

  • June 25, 2025
  • 4 replies
  • 0 views

Please observe the result of these forEach, (1. and 2.):

1.

a!forEach(
   items: {1,2,3,4,5},
   expression:
   if ( fv!item > 3,
     fv!item,
     {}
   )

)

results in 

meanwhile

2. 

a!forEach(
   items: {1,2,3,4,5},
  expression:
  if ( fv!item > 3,
    fv!item,
    null
   )

)

results in 

Question: Why forEach 1. does not outcome five members in the list?, I expected this result

  • List of Variant - 0 items
      • List of Variant - 0 items
          • List of Variant - 0 items
          • 4(Number (Integer))
          • 5(Number (Integer))

          4 replies

          mikes0011
          Brainy
          June 25, 2025

          This is the expected result.  In your first example, the fallback value is set as an empty set (i.e. an array of size zero), which by standard Appian convention is excluded from array result sets (at least when the entire array contains any non-empty value).  This is very useful when it comes to creating a trimmed array where the only members meet a certain condition, since the negative case can typically just be set to an empty set such as is seen here.

          In your second example, on the other hand, the fallback value is a single null, which is very different from an empty set - a null is considered to be (in terms of data type) a single value, just with a blank value, but that doesn't stop its data type from being singular.  This is often used for example to insert nulls into arrays (or replace items in an array with null) wherein it's important for the array to maintain its original size/shape, but for individual items to be excluded due to whatever reasons are given in the logic.

          Further I'd clarify that this doesn't really have anything in particular to do with a!forEach(), but rather, the behavior of empty sets and nulls in arrays overall (since at the end of the day, a!forEach() just generates an array).  A simple example shows this pretty clearly:

          shubhama926776
          June 26, 2025

          a!forEach() automatically flattens results.

          {} (Empty array) -> Gets flattened away
          null -> Get preserved

          mikes0011
          Brainy
          June 26, 2025
          a!forEach() automatically flattens results.

          There's actually an important corner case that would make me hesitant to put it this simply - since it specifically *doesn't* (exactly) flatten results - that is, when the result is all empty sets, it will return an array of empty sets (where the average user might want to have just gotten back an empty array).  When this is a possibility, it's necessary to wrap the a!forEach() call in a!flatten() which actually does flatten the result.

          meghar3159
          June 26, 2025

          Hi Rafael,
          This is the expected result and this is a subtle behavior in a!forEach that often catches people off guard.
          Here’s the difference:

          • {} is treated as an empty list, so a!forEach skips that item entirely.

          • null is treated as a value, so it's included in the output list.

          That’s why in example 1, only 4 and 5 appear, the others were dropped because {} returns nothing. Using null ensures the full list structure is preserved.

          Hope that helps!