Array Expression

Certified Associate Developer

Hi guys/gals,
Some help please with an expression rule I'm trying to create that's driving me nuts.
Here's the simple example:

a!localVariables(
local!array:
{
  { id: 1, deleted: true, activeStep: false },
  { id: 2, deleted: false, activeStep: false },
  { id: 3, deleted: false, activeStep: false },
},

local!array
)

I'm passing a list of Dictionary to the expression (local!array in the example above). I want to return an updated local!array where the activeStep is set to true for the first item where deleted is false.
In my example above, the second row will have its activeStep set to true as the first row's deleted field is true. The third row will remain as is because the second row's activeStep field will be already set to true.

So the returned value should be:
{
  { id: 1, deleted: true, activeStep: false },
  { id: 2, deleted: false, activeStep: true },
  { id: 3, deleted: false, activeStep: false },
}

Seems simple but if I use a!forEach, both the 2nd and 3rd row's activeStep is set to true. I only want the first occurrence.

Cheers...

  Discussion posts and replies are publicly visible

Parents
  • Try it using a!foreach but change your logic like this,

    a!foreach(

    items: local!array,

    expression: if(fv!index = 1,

    set the value which you want to true , else fv!item)

  • 0
    Certified Associate Developer
    in reply to harshav

    Hi harshav, Thanks for that. Problem is it's not always the first index. It's the minimum index where the deleted field is false.

    I just got this working below by finding that minimum index first, but would love to know if there's a more ideal way.

    a!localVariables(
      local!array:
      {
        { id: 1, deleted: true, activeStep: false },
        { id: 2, deleted: false, activeStep: false },
        { id: 3, deleted: false, activeStep: false },
      },
      local!rowToUpdate: min(wherecontains("false", split(tostring(local!array.deleted), "; "))),
      
      a!forEach(
        local!array,
        if(fv!index=local!rowToUpdate,
          {id: fv!item.id, deleted: fv!item.deleted, activeStep: true},
          fv!item
        )
      )
    )

Reply
  • 0
    Certified Associate Developer
    in reply to harshav

    Hi harshav, Thanks for that. Problem is it's not always the first index. It's the minimum index where the deleted field is false.

    I just got this working below by finding that minimum index first, but would love to know if there's a more ideal way.

    a!localVariables(
      local!array:
      {
        { id: 1, deleted: true, activeStep: false },
        { id: 2, deleted: false, activeStep: false },
        { id: 3, deleted: false, activeStep: false },
      },
      local!rowToUpdate: min(wherecontains("false", split(tostring(local!array.deleted), "; "))),
      
      a!forEach(
        local!array,
        if(fv!index=local!rowToUpdate,
          {id: fv!item.id, deleted: fv!item.deleted, activeStep: true},
          fv!item
        )
      )
    )

Children