Remove duplicate values in grid

Hi All,

I have requirement where I need to remove consequent duplicate value while showing the data in grid.

i,e if values are: {"Requested", "Requested", "Pending","Sent For Approval","Approved","Pending","Approved","Approved"} the expected output is {"Requested", , "Pending","Sent For Approval","Approved","Pending","Approved"}

Here "Requested" and "Approved" are getting rejected as they are consequently occurring twice.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Associate Developer

    Hi Gaurav,

    You can get unique list first and then pass that to Grid.

    EX:{1,2,3,4,1,4,5}

    try
    union({1,2,3,4,1,4,5},{1,2,3,4,1,4,5}) returns :{1,2,3,4,5}

  • 0
    A Score Level 2
    in reply to Ram K

    Hi Leela,

    Requirement is to remove two consecutive duplicates, not all duplicates 

    {"Requested", "Requested", "Pending", "Sent For Approval", "Approved", "Pending", "Approved", "Approved"} the expected output is {"Requested", "Pending", "Sent For Approval", "Approved", "Pending", "Approved"}

  • 0
    Certified Lead Developer
    in reply to GauravSingh

    You use reduce() to do this. Requires a helper function. They idea is:

    If last item in list equals new value, skip it, else append it.

    MAIN

    a!localVariables(
      local!values: {"Requested", "Requested", "Pending", "Sent For Approval", "Approved", "Pending", "Approved", "Approved"},
      reduce(
        rule!SSH_ReduceHelper(
          list:_,
          newValue:_
        ),
        {},
        local!values
      )
    )

    HELPER: Inputs: list(Array of Text), newValue(Text)

    if(
      index(reverse(ri!list), 1, "") = ri!newValue,
      ri!list,
      append(ri!list, ri!newValue)
    )

  • +1
    Certified Lead Developer
    in reply to Stefan Helzle

    Pretty sure a!forEach() can handle this implicitly, without needing to involve legacy looping functions or helper rules.  E.G.:

    a!localVariables(
      local!values: {"Requested", "Requested", "Pending", "Sent For Approval", "Approved", "Pending", "Approved", "Approved"},
      
      a!forEach(
        local!values,
        if(
          fv!isFirst,
          fv!item,
          if(
            fv!item = local!values[fv!index - 1],
            {},
            fv!item
          )
        )
      )
    )

    Result:

    Note that this would collapse *all* consecutively repeated values, not just cases where there are 2 repeated items.  Though it sounds like this satisfies OP's use case anyway.

Reply
  • +1
    Certified Lead Developer
    in reply to Stefan Helzle

    Pretty sure a!forEach() can handle this implicitly, without needing to involve legacy looping functions or helper rules.  E.G.:

    a!localVariables(
      local!values: {"Requested", "Requested", "Pending", "Sent For Approval", "Approved", "Pending", "Approved", "Approved"},
      
      a!forEach(
        local!values,
        if(
          fv!isFirst,
          fv!item,
          if(
            fv!item = local!values[fv!index - 1],
            {},
            fv!item
          )
        )
      )
    )

    Result:

    Note that this would collapse *all* consecutively repeated values, not just cases where there are 2 repeated items.  Though it sounds like this satisfies OP's use case anyway.

Children
  • Thanks Mike,

    This really helped with small variation as I was using one cdt where two fields needs to be considered together.

    a!localVariables(
    local!RequestHistory:###Query From DB###,
    local!uniqueHistoryData:a!forEach(
        local!colocationRequestHistory,
        if(
          fv!isFirst,
          fv!item,
          if(
            and(
            fv!item.statusExternal = local!RequestHistory.statusExternal[fv!index - 1],
            fv!item.stage = local!RequestHistory.stage[fv!index - 1],
            ),
            {},
            fv!item
          )
        )
      ),
      local!uniqueHistoryData
      )