Skip to main content
rishukumarg1965
June 16, 2025
Solved

if duplicate data exists in the list then display a message indicating that duplicate data is present.

  • June 16, 2025
  • 7 replies
  • 0 views

local!data: {
a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"), 
a!map(SAPNumber: "1111111", Qty: "2", Spare: "1"),
a!map(SAPNumber: "1111111", Qty: "3", Spare: "0"),
a!map(SAPNumber: "2222222", Qty: "1", Spare: "0")
},

In the above list, If a duplicate combination of 'SAPNumber' and 'Spare' exists in the list, display a message indicating that duplicate data is present.

Example : 

a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"), 

a!map(SAPNumber: "1111111", Qty: "3", Spare: "0"),

here, 'SAPNumber' and 'Spare' are same so it must shows a message.

    Best answer by pavans123939

    a!localVariables(
      local!data: {
        a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"), 
        a!map(SAPNumber: "1111111", Qty: "2", Spare: "1"),
        a!map(SAPNumber: "1111111", Qty: "3", Spare: "0"),
        a!map(SAPNumber: "2222222", Qty: "1", Spare: "0")
      },
      
     a!forEach(
        items:local!data,
        expression:a!localVariables(
          local!innerData:index(local!data,fv!index,null()),
          local!SAPnumber:property(local!innerData,"SAPnumber"),
          local!spare:property(local!innerData,"Spare"),
          if(
            contains(
              property(remove(local!data,fv!index),"SAPnumber"),
              local!SAPnumber
            ),
            if(
              contains(
                property(remove(local!data,fv!index),"Spare"),
                local!spare
              ),
              "Duplicates Found",
              null()
            ),
            null()
           )
          )
        )
     )
    If the primary goal is to show a validation message when duplicates exist in the map list, Harsha's approach is effective. However, if identifying the precise position of each duplicate is required, this method would be more suitable. Feel free to adjust the code to fit your specific use case.

    7 replies

    harshas2775
    June 16, 2025

    You can build an expression rule to check duplicates and call that in the validations section of the grid/section. 

    a!localVariables(
      local!data: {
        a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"),
        a!map(SAPNumber: "1111111", Qty: "2", Spare: "1"),
        a!map(SAPNumber: "1111111", Qty: "3", Spare: "0"),
        a!map(SAPNumber: "2222222", Qty: "1", Spare: "0")
      },
      if(
        contains(
          a!foreach(
            local!data,
            a!localVariables(
              local!arrayTemp: remove(local!data, fv!index),
              local!SAPCheck: property(
                local!arrayTemp,
                wherecontains(
                  fv!item.SAPNumber,
                  local!arrayTemp.SAPNumber
                ),
                {}
              ),
              local!spareCheck: property(
                local!arrayTemp,
                wherecontains(fv!item.Spare, local!arrayTemp.Spare),
                {}
              ),
              a!isNullOrEmpty(
                intersection(local!SAPCheck, local!spareCheck)
              ),
              
            )
          ),
          true()
        ),
        "Validation Message",
        {}
      )
    )

    harshas2775
    June 16, 2025

    a!localVariables(
      local!data: {
        a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"),
        a!map(SAPNumber: "1111111", Qty: "2", Spare: "1"),
        a!map(SAPNumber: "1111111", Qty: "3", Spare: "2"),
        a!map(SAPNumber: "2222222", Qty: "1", Spare: "0")
      },
      local!dataset: a!foreach(
        local!data,
        a!localVariables(
          local!arrayTemp: remove(local!data, fv!index),
          local!SAPCheck: property(
            local!arrayTemp,
            wherecontains(
              fv!item.SAPNumber,
              local!arrayTemp.SAPNumber
            ),
            {}
          ),
          local!spareCheck: property(
            local!arrayTemp,
            wherecontains(fv!item.Spare, local!arrayTemp.Spare),
            {}
          ),
         
            a!isNotNullOrEmpty(
              intersection(local!SAPCheck, local!spareCheck)
            )
    
        )
      ),
      if(
        contains(local!dataset, true()),
        "Validation Message",
        ""
      )
    )

     [mention:c7ed152842da4892ba88b9d946100b8f:e9ed411860ed4f2ba0265705b8793d05] I noticed an issue in the above code while testing with a negative dataset, here is an adjusted one to resolve that. Pls refer this one. 

    rishukumarg1965
    June 16, 2025

    Thankyou [mention:65b14048184a4eb5aff3a76c87b97431:e9ed411860ed4f2ba0265705b8793d05] [emoticon:33306c418930400bac28808410f8ac8b]

    June 16, 2025

    a!localVariables(
      local!data: {
        a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"), 
        a!map(SAPNumber: "1111111", Qty: "2", Spare: "1"),
        a!map(SAPNumber: "1111111", Qty: "3", Spare: "0"),
        a!map(SAPNumber: "2222222", Qty: "1", Spare: "0")
      },
      
     a!forEach(
        items:local!data,
        expression:a!localVariables(
          local!innerData:index(local!data,fv!index,null()),
          local!SAPnumber:property(local!innerData,"SAPnumber"),
          local!spare:property(local!innerData,"Spare"),
          if(
            contains(
              property(remove(local!data,fv!index),"SAPnumber"),
              local!SAPnumber
            ),
            if(
              contains(
                property(remove(local!data,fv!index),"Spare"),
                local!spare
              ),
              "Duplicates Found",
              null()
            ),
            null()
           )
          )
        )
     )
    If the primary goal is to show a validation message when duplicates exist in the map list, Harsha's approach is effective. However, if identifying the precise position of each duplicate is required, this method would be more suitable. Feel free to adjust the code to fit your specific use case.

    rishukumarg1965
    June 16, 2025

    Thankyou [mention:3a3343a371904c5bb84f4b9bada8af02:e9ed411860ed4f2ba0265705b8793d05] 

    stefanhelzle0001
    June 16, 2025

    A simple approach is to create a list of combined keys, get the unique combined keys, and then compare the number of combined keys to the number of items in your list. Below the code:

    a!localVariables(
      local!data: {
        a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"),
        a!map(SAPNumber: "1111111", Qty: "2", Spare: "1"),
        a!map(SAPNumber: "1111111", Qty: "3", Spare: "0"),
        a!map(SAPNumber: "2222222", Qty: "1", Spare: "0")
      },
      local!combinedKey: a!forEach(
        items: local!data,
        expression: concat(fv!item.SAPNumber, "-", fv!item.Spare)
      ),
      if(
        count(local!data) <> count(union(local!combinedKey, local!combinedKey)),
        "Validation Message",
        ""
      )
    )

    sureshd0004
    June 16, 2025

    Another simple way to do this is as below, Note: I have not handle any null errors etc.. just a targeted answer for the requirement

    a!localVariables(
      local!data: {
        a!map(SAPNumber: "1111111", Qty: "1", Spare: "0"),
        a!map(SAPNumber: "1111111", Qty: "2", Spare: "1"),
        a!map(SAPNumber: "1111111", Qty: "3", Spare: "0"),
        a!map(SAPNumber: "2222222", Qty: "1", Spare: "0")
      },
      if(
        length(local!data) > length(
        /* This union can be a rule named like XXX_removeDuplicate() */
          union(
            merge(local!data.SAPNumber, local!data.Spare),
            merge(local!data.SAPNumber, local!data.Spare)
          )
        ),
        "Error Message",
        ""
      )
    )