Skip to main content
July 22, 2021
Question

Index default value

  • July 22, 2021
  • 7 replies
  • 0 views

Hi,

I have this code where if the index points are not returned, it should return the default value. But instead it returns empty data.

How do I make sure that the default value is returned

a!localVariables(
  
  local!address1: {{id:1},{id:2}},
  local!mdmAddress:{{id:5,name:"abc"},{id:3,name:"pqr"}},
  
  index(local!mdmAddress,
  wherecontains(local!address1.id,local!mdmAddress.id),"Hello"
  )
 
)

    7 replies

    selvakumark
    Participating Frequently
    July 22, 2021

    Hi [mention:08e7de8b1e874627b6bb409dbce962cd:e9ed411860ed4f2ba0265705b8793d05]

    The default value should be the same as that of the array elements as per the documentation.

    docs.appian.com/.../fnc_array_index.html

    raghuvarann456
    August 4, 2021

    Hi,

    One of the weird behaviours of index(), When you pass empty array as index it returns all values in the array because it treats the index as a list of empty nested field names (text).

    But if you pass empty integer array as index it returns empty.

    wherecontains() has a return type as integer array even it's empty. so index will return empty.

    please check this snippet below.

    index({"A","B"},{},{"default"}) /*returns A,B */

    index({"A","B"},tointeger({}),{"default"})  /*returns empty */

    harshitb6843
    August 4, 2021

    Hi There,

    In such cases, it is always best to add a null check. 
    E.g. Instead of retuning "Hello", please return a null and then in another variable, using an if condition, add a null check to return "hello" in case the indexed property is null or does not exists. 

    Also, please consider creating a custom nullCheck rule that treats null, empty string, and empty list as null. 

    mikes0011
    Brainy
    August 4, 2021
    using an if condition

    And now in 21.2 they've made this even easier since we have a!defaultValue(), such things no longer need to be wrapped in an IF() statement.

    The Expression Guru
    harshitb6843
    August 5, 2021

    Oh yes! How can I forget that [emoticon:c22324d72be24d45bf071047c6583446]
    But I wonder, does it checks for blank string and blank list too?

    August 4, 2021

    Thanks everyone for your inputs. i have tried a work around, where i am doing a null check for the index array, but I was just wondering index function itself does the null check and returns the default value, but it's not doing in this case.