Checking for duplicates in an array or list

Certified Senior Developer

Hi

I want to check for duplicates in Supplier list when I am adding a new supplier. 

I tried using this expresssion:

 

a!textField(
                label: "Supplier Name",
                value: fv!item.description,
                saveInto: fv!item.description,
                validations:
                {
                  if(
                    count(wherecontains(local!suppliers,index(fv!item,fv!index,{})))>1,
                    "Supplier already exists",{}
)
},

But I am getting the error -> Could not display interface. Please check definition and inputs. Interface Definition: Expression evaluation error at function a!forEach [line 39]: Error in a!forEach() expression during iteration 1: Expression evaluation error at function 'wherecontains' [line 52]: Invalid types, can only act on data of the same type (Any Type, Text)

I tried the methods mentioned in earlier posts but not able to fix it yet. Can someone help me?

Thanks

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    Hi, first thing you need to use touniformstring () as Danny said ,to make sure you are working on same type of data. Apart from this why are you indexing fv!index from fv!items? I think you need to correct this too.

  • 0
    Certified Senior Developer
    in reply to GopalK

    Hi Gopal,

    I am trying the check for duplicates. 

    Should I use the column name directly instead of fv!index?

    Thanks

  • 0
    Certified Senior Developer
    in reply to Sandhya

    If you are storing supplier name in fv!item.description then you should use this field only to check whether value provided by user exists in local! suppliers or not.i assume you have list of suppliers in local!suppliers then your expression should be like below,

    WhereContains(touniformstring (local! suppliers),fv!item.description)

  • 0
    Certified Lead Developer
    in reply to GopalK

    also it would be "local!suppliers.description", since local!suppliers is apparently a dictionary containing a "description" field that's the target of the text field in question.

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Hi Mike,

    I tried that too. 

    I am getting this error: 

    Could not display interface. Please check definition and inputs. Interface Definition: Expression evaluation error at function a!forEach [line 39]: Error in a!forEach() expression during iteration 1: Expression evaluation error at function 'wherecontains' parameter 1 [line 51]: Invalid index: Cannot index property 'description' of type Text into type List of Variant

     validations: {
                      if(
                        count(
                          wherecontains(
                            touniform(local!suppliers.description),
                            fv!item.description
                          )
                        ) > 1,
                        "Supplier already exists",
                        {}
                      )
                    },

    I tried with and without nouniformstring().

    I was working on it yesterday and it worked. Don't what I changed to get the error today.

    Thanks

  • 0
    Certified Senior Developer
    in reply to Sandhya

    There is typo in your code. "touniform"

  • 0
    Certified Lead Developer
    in reply to Sandhya

    If the dictionary is empty then the ".description" property might not evaluate correctly before an initial entry is added.

    Also the function is "touniformstring()", not "touniform()" like you have in your code currently.

  • 0
    Certified Lead Developer
    in reply to Sandhya

    Additionally, whereContains() requires the unique item (and/or item(s)) you're trying to find the positions of in the longer/original array, to be listed first in the function, not second.

    And if your local!suppliers is a generic dictionary, the .description property will probably need to be typecast because whereContains() is picky about types.

    Thus, it would be properly written like this:

    wherecontains(
      tostring(fv!item.description),
      touniformstring(local!suppliers.description)
    )

  • 0
    Certified Lead Developer
    in reply to Sandhya

    I whipped up my own version of this real quick and it appears to be working fine.

    a!localVariables(
      
      local!suppliers: {},
      
      a!gridLayout(
        headerCells: {
          a!gridLayoutHeaderCell(label: "Description")
        },
        
        rows: a!forEach(
          local!suppliers,
          a!gridRowLayout(
            contents: {
              a!textField(
                value: fv!item.description,
                saveInto: fv!item.description,
                validations: if(
                  count(
                    wherecontains(
                      tostring(fv!item.description),
                      touniformstring(local!suppliers.description)
                    )
                  ) > 1,
                  "Supplier already exists",
                  ""
                )
              )
            }
          )
        ),
        
        addRowLink: a!dynamicLink(
          label: "Add",
          saveInto: a!save(
            local!suppliers,
            append(
              local!suppliers,
              {
                id: 1,
                description: ""
              }
            )
          )
        )
      )
    )

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Hi Mike,

    That's great!

    But sadly, isn't working for me still. 

    a!localVariables(
      local!QRSuppliers: a!queryEntity(
        entity: cons!BC_SUPPLIER_DSE,
        query: a!query(
          selection: a!querySelection(
            columns: {
              a!queryColumn(field: "supplierId"),
              a!queryColumn(field: "description"),
              a!queryColumn(field: "displayOrder"),
              a!queryColumn(field: "isActive"),
              a!queryColumn(field: "createdDate"),
              a!queryColumn(field: "createdBy"),
              a!queryColumn(field: "modifiedDate"),
              a!queryColumn(field: "modifiedBy"),
              
            }
          ),
          pagingInfo: a!pagingInfo(startIndex: 1, batchSize: - 1)
        ),
        fetchTotalCount: true
      ).data,
      local!deletedSuppliers,
      local!addedSuppliers,
      local!suppliers: local!QRSuppliers,
      a!formLayout(
        label: "Manage Supplier",
        contents: {
          a!gridLayout(
            headerCells: {
              a!gridLayoutHeaderCell(label: "Supplier Name"),
              a!gridLayoutHeaderCell(label: "Active"),
              a!gridLayoutHeaderCell(label: "")
            },
            columnConfigs: {
              a!gridLayoutColumnConfig(width: "DISTRIBUTE", weight: 3),
              a!gridLayoutColumnConfig(width: "DISTRIBUTE", weight: 3),
              a!gridLayoutColumnConfig(width: "ICON")
            },
            rows: a!forEach(
              items: local!suppliers,
              expression: a!gridRowLayout(
                contents: {
                  a!textField(
                    label: "Supplier Name",
                    value: fv!item.description,
                    saveInto: fv!item.description,
                    validations: if(
                      count(
                        wherecontains(
                          tostring(fv!item.description),
                          touniformstring(local!suppliers.description)
                        )
                      ) > 1,
                      "Supplier already exists",
                      ""
                    ),
                    required: true

    Where am I making the mistake?

    Thanks

  • +1
    Certified Lead Developer
    in reply to Sandhya

    It's hard to tell without knowing what the value of local!suppliers is, which apparently depends on the result of your initial query.  It looks, though, as if the data rows you're getting back don't contain a ".description" property.  The error message we're seeing here is the standard error message received when you try to take a dot property from a dictionary/CDT for a property it doesn't have.

    To circumvent this, we can try to get the dot property safely by using the property() function, i.e.

    touniformstring(property(local!suppliers, "description", {}))

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    At last, It worked. 

    Thank you so much Mike and others for help. 

    Have a good day

Reply Children
No Data