Duplicate check in a dynamic grid layout

Hi, I have a text field in dynamic grid layout. How do i check for a duplicate entry and generate a error message in validation?

(I'm storing the entries from the text field in a rule input of cdt type)

  Discussion posts and replies are publicly visible

  • Hi Mohit,

    If i am right here, then you are adding a new row then you want to do duplicate check,right? if yes then create a rule input array type and save each value of that text field so if you added three new row then this variable will hold three different values(till now i added different values in text field). Now use the below code for validation:

    in the saveInto use the below code:

    a!save(
    ri!roles,
    append(
    ri!roles,
    fv!item.roleName
    )
    )
    and for validation use below :

    if(
    count(
    index(
    ri!roles,(your variable name where you need to store each value)
    wherecontains(
    fv!item.roleName, (your text field value)
    ri!roles
    ),
    0
    )
    ) >= 2,
    "Please select different Value",
    ""
    )



    Regards
    Abhay
  • Hi Abhay,

    Thanks for that. It works. But there are still some issues with that.

    Like:
    If you edit the existing duplicate textbox instead of deleting that row then it will still throw an error because when you edit the existing textbox then another entry is appended in the ri!role input instead of replacing the existing index value.

    Regards,
    Mohit
  • Hi Mohit,

    for this error , what you can do instead of saving , you just replace the value based on Index for the existing record then i think your concern will be resolved , and still you receive error please let me know , i will share the code as well with you.

    Regards
    Abhay Giri
    Mark this reply as answered or vote if this helps you.
  • Hi Mohit

    You can try this below code to check the duplicate in the dynamic grid layout

    forEach(
    items:ri!array,
    expression:
    textField(
    validations:{
    if(
    count(wherecontains(ri!array,index(fv!item,fv!index,{} ))) >1,
    "Duplicate",{}
    )
    }
    )
    )

    In place of fv!index you can use the column name if the ri!array is of multiple CDT type
  • Thanks Abhay . That was helpful but i got a much optimized version of it.

    Regards,
    Mohit Shah
  • Hi Arpit,

    That was a really short version of that and worked completely fine. There's just a slight change for multiple cdt though.

    For multiple cdt instead of replacing fv!index we can replace the index function all together with the column name.
    Here's what it it looks like for me for multiple cdt,

    if(count(wherecontains(fv!item.(columnName), ri!array.(columnName))) > 1
    ,"Duplicate",
    {}
    )

    Please ignore the brackets with column name.

    Regards,
    Mohit Shah
  • 0
    Certified Senior Developer
    in reply to Abhay Giri

    Can you please share the code for this

  • 0
    Certified Lead Developer
    in reply to aayushib0002
    Can you please share the code for this

    FYI this is a super-old thread - nearly 5 years now - if you're having a specific issue, it might be appropriate to start your own new post now if there are no other/more recent threads that seem to deal with your issue.

  • Hi Arpit

      a!localVariables(
        local!data:rule!D_Demo_excel_Data(data:ri!input),
        {
          
          a!gridLayout(
            label: "Student Data",
            labelPosition: "ABOVE",
            headerCells: {
              a!gridLayoutHeaderCell(label: "Roll_No"),
              a!gridLayoutHeaderCell(label: "Name"),
              a!gridLayoutHeaderCell(label: "Subject_1"),
              a!gridLayoutHeaderCell(label: "Subject_2"),
              a!gridLayoutHeaderCell(label: "Subject_3"),
              a!gridLayoutHeaderCell(label: "Subject_4"),
              a!gridLayoutHeaderCell(label: "Total_Percentage"),
              a!gridLayoutHeaderCell(label: "Grade"),
            },
            columnConfigs: {},
            rows: {
              a!forEach(
                items: ri!data,
                expression: a!localVariables(
                  a!gridRowLayout(
                    contents: {
                      a!integerField(
                        value: fv!item.Roll_No,
                        saveInto: fv!item.Roll_No,
                        readOnly: true
                      ),
                      a!textField(
                        value: fv!item.Name,
                        saveInto: {fv!item.Name,a!save(fv!item.Total_Percentage,{((fv!item.Subject_1) + (fv!item.Subject_2) + (fv!item.Subject_3) + (fv!item.Subject_4) )/4}),a!save(fv!item.Grade,rule!D_excelGrade(percentage: fv!item.Total_Percentage))},
                        required: true
                      ),
                      a!integerField(
                        value: fv!item.Subject_1,
                        saveInto: {fv!item.Subject_1,a!save(fv!item.Total_Percentage,{((fv!item.Subject_1) + (fv!item.Subject_2) + (fv!item.Subject_3) + (fv!item.Subject_4) )/4}),a!save(fv!item.Grade,rule!D_excelGrade(percentage: fv!item.Total_Percentage))},
                        required: true
                      ),
                      a!integerField(
                        value: fv!item.Subject_2,
                        saveInto: {fv!item.Subject_2,a!save(fv!item.Total_Percentage,{((fv!item.Subject_1) + (fv!item.Subject_2) + (fv!item.Subject_3) + (fv!item.Subject_4) )/4}),a!save(fv!item.Grade,rule!D_excelGrade(percentage: fv!item.Total_Percentage))},
                        required: true
                      ),
                      a!integerField(
                        value: fv!item.Subject_3,
                        saveInto: {fv!item.Subject_3,a!save(fv!item.Total_Percentage,{((fv!item.Subject_1) + (fv!item.Subject_2) + (fv!item.Subject_3) + (fv!item.Subject_4) )/4}),a!save(fv!item.Grade,rule!D_excelGrade(percentage: fv!item.Total_Percentage))},
                        required: true
                      ),
                      a!integerField(
                        value: fv!item.Subject_4,
                        saveInto: {fv!item.Subject_4,a!save(fv!item.Total_Percentage,{((fv!item.Subject_1) + (fv!item.Subject_2) + (fv!item.Subject_3) + (fv!item.Subject_4) )/4}),a!save(fv!item.Grade,rule!D_excelGrade(percentage: fv!item.Total_Percentage))},
                        required: true
                      ),
                      a!integerField(
                        value:
                        {((fv!item.Subject_1) + (fv!item.Subject_2) + (fv!item.Subject_3) + (fv!item.Subject_4) )/4},
                        /*saveInto:fv!item.Total_Percentage,*/
                       readOnly: true(),
                        required: true
                      ),
                      a!textField(
                        value: rule!D_excelGrade(percentage:fv!item.Total_Percentage),
                        /*saveInto:fv!item.Grade,*/
                        readOnly: true(),
                        required: true
                      ),
                    }
                  )
                )
              )
            },
            selectionSaveInto: {},
            validations: {},
            shadeAlternateRows: true
          ),
          a!buttonArrayLayout(
            buttons: {
              a!buttonWidget(
                label: "save",
    
                
                submit: true,
                style: "NORMAL"
              )
            },
            align: "START"
          )
          
        }
       
      )
    

    I am Getting the data from a excel file and writing it to the DB. but i want to check for duplicate, like if users enters the same record they must not be written in to the DB.