if statement - care required with parameters that could be lists

Certified Associate Developer

I had written some logic that queried a record (expecting, and getting a single record, by passing a unique filter value)

The query actually could return multiple records, so returns a List of the relevant cdt


Then I tested a boolean field in that record is true or false, and returns one of two lists of items, for example something like this:

if (
  index(my_cdt, "booleanField",false),
  list_one,
  list_other
)

My list was being returned but only with the first item in it!

The issue is that the query returns a list of records, so the if statement parameter 1 is on a list type.

An example of the logic I was actually executing was therefore:

if(
    {true},
    {1,2,3},
    {4,5,6}
)

This actually returns {1}, not {1,2,3}

if(
  true,
  {1,2,3}
  {4,5,6}
)

will return {1,2,3} as expected.

Further example to show what's actually happening:

if(
  {true,false,true},
  {1,2,3},
  {4,5,6}
)
  

This returns {1,5,3} !!

An easy mistake to make, so hopefully some one will read this and not fall into the trap I did.

  Discussion posts and replies are publicly visible