How to remove an item from a list depending of a field value

Certified Senior Developer

Hi,

I would like to remove an item from a list, depending on a field (here, the condition is : when the "checked" field is false, I need to remove the item) :

/* Set to null the unchecked Vehicles items */
a!forEach(
  items: ri!vehicles,
  expression: if (rule!SHARED_IsFalse(fv!item.checked),
    a!save(fv!item, null),
    a!save(fv!item, fv!item)
  )
),
/* Remove the null items */
a!save(ri!vehicles, reject(fn!isnull, ri!vehicles)),


1/ How would you code it in a single instruction please ?  (with a foreach and a remove ?)

2/ From a SaveInto of an interface the code above works fine, but now I'm trying to execute it from an Expression Rule. So, how would you code this instruction "a!save(fv!item, null)", as a!save cannot be used in ER ?

I've tried to use the function updateDictionary(), but I do not see how to set an item to null (I can just set some fields to a null value).

I'm sure the answer of the first question can make the 2/ completely unnecessary ;-)


Regards

  Discussion posts and replies are publicly visible

  • 0
    Certified Senior Developer

    I've tried to delete this post but did not succeed. I think I've found a good way to do it :

    remove(ri!vehicles,
    wherecontains(false, ri!vehicles.checked)
    ),

  • 0
    Certified Lead Developer

    I see you've found a working solution but I will add some explanation as to your original code and why it would not be expected to work:  a!save() operations only execute on direct user interaction, i.e. in the saveInto parameter of a user input field (text field, button, etc, etc).  It won't do anything when called directly within an expression function like you have.

    Your remove() example will work, though I prefer to avoid the old array functions where i can and instead use a!forEach() which is far more flexible.  A working a!forEach() example is as follows:

    a!forEach(
      items: ri!vehicles,
      expression: if(
        fv!item.checked,
        fv!item,
        {}
      )
    )

    What this is essentially doing is assembling a new list of items based on the ri!vehicles list, but only including the current item if the ".checked" property is TRUE.  (This would also exclude any items where the checked property is blank, though you can easily adjust your code to suit this if it's a possibility).  When fv!item.checked is not true, then the loop returns an empty set {} which will be ignored in the resulting array created.  If there's a chance that all items will need to be excluded, then you should also wrap the entire expression in a!flatten().

  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    I've kind of gotten into the habit of wrapping things in a!flatten() automatically without really thinking about it.

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Thanks a lot Mike, that is a very interesting explanation.