Skip to main content
hema.mathivathanan
June 28, 2023
Solved

Duplicate in the grid

  • June 28, 2023
  • 3 replies
  • 0 views

Hi, 

I have a grid layout and the values are populated from my selection. I need to check, if there are duplicated values present in the grid. If there are duplicate values present, I should not able to submit the form. I store my selection in a local variable. 

validations: { 

if(

local!data.value ---> here I need to check if the value is already present in the grid. 

"pleaser remove duplicates",

""

)

}

Can someone please help how to check if the values are already present.  

    Best answer by mikes0011

    You can add a validation item on your grid, which checks whether there are duplicate values within that column.  I wrote a rule a while ago which looks into an array and returns any items that are duplicated within the array any number of times, which I'll share below.

    For the validation message on your grid:

    a!validationMessage(
      message: "Duplicate Amounts are present.",
      validateAfter: "SUBMIT",
      showWhen: length(
        rule!GLBL_getArrayDuplicates(local!gridData.Amount)
      ) > 0
    )

    For the rule GLBL_getArrayDuplicates():

    a!flatten(
      a!forEach(
        ri!array,
        if(
          length(wherecontains(
            fv!item,
            cast(typeof({fv!item}), ri!array)  /* (this allows us to handle a mixed-type array properly) */
          )) > 1,
          fv!item,
          {}
        )
      )
    )

    3 replies

    mikes0011
    Brainy
    June 28, 2023

    What would "duplicate values" look like in this case?  Are you talking about a grid of data being created / populated by the user at run-time, or something else?

    The Expression Guru
    hema.mathivathanan
    June 28, 2023

    In the grid, for a particular field I select values from another table. The selected value appears in the grid. 
    Now, I am able to select the same value twice. But, it should not happen.

    For example, if I have the same value twice, it should throw the error, "duplicate values are present". please check

    mikes0011
    mikes0011Answer
    Brainy
    June 28, 2023

    You can add a validation item on your grid, which checks whether there are duplicate values within that column.  I wrote a rule a while ago which looks into an array and returns any items that are duplicated within the array any number of times, which I'll share below.

    For the validation message on your grid:

    a!validationMessage(
      message: "Duplicate Amounts are present.",
      validateAfter: "SUBMIT",
      showWhen: length(
        rule!GLBL_getArrayDuplicates(local!gridData.Amount)
      ) > 0
    )

    For the rule GLBL_getArrayDuplicates():

    a!flatten(
      a!forEach(
        ri!array,
        if(
          length(wherecontains(
            fv!item,
            cast(typeof({fv!item}), ri!array)  /* (this allows us to handle a mixed-type array properly) */
          )) > 1,
          fv!item,
          {}
        )
      )
    )

    The Expression Guru