Enforce unique entries on multiple columns using JavaScript

  • 6 February 2018
  • 3 replies
  • 16 views

Badge +5

Hello,

I want to prevent duplicate entries rather than clean them up with a workflow afterwards.  SharePoint only allows for this on one field per row.  I need to check at least two columns per row to determine if an entry is unique.  I've been searching Nintex Community and Google and came across a script that uses ajax to check field values in a list. I've adjusted the script to suit my list and tested it using a default SharePoint form.  It worked very nicely.  However, when I tested this same script (with some adjustments to selectors to suit the class names I added in the Nintex form), the script notifies the user that their entry is a duplicate but then still saves the item to the list.  Is there something significantly different about the "Submit" process with Nintex forms that would cause this?  When the script encounters a duplicate in the default SharePoint form, the "Save" button doesn't proceed with saving the item.

This is what my script looks like:

    // adding javascript to button onclick event to call checkExist() function, saves existing onclick event to variable
    var oc = $('input[value="Submit"]').attr('onclick');
    $('input[value="Submit"]').attr('onclick','if (checkExist()){alert("Employee already exists!");} else {'+ oc +'}');

    //variable to hold Ajax result
    var dataResults;
   
    //Ajax to populate data from list
    $.ajax({
        url: "https://nameofsite/_api/web/lists/getbytitle('New%20Employee%20Request')/items?$select=n9xv,Title",
        type: "GET",
        async: false,
        headers: { "Accept": "application/json;odata=verbose" },
        success: function(data) {
            dataResults = data.d;
        }   
    });


    function checkExist(){
        var empName = $('.employee-name-control').val();
        var title = $('.position-title-control').val();   
       
        var c = null;
       
        // get list items, n9xv is position title, title is employee name
        $.each(dataResults, function(i, item){
            $.each(item, function(i, dt){
                if (dt.Title == empName && dt.n9xv == title){
                    c = true;
                    return false;
                }
            });
        });
        return c; 

    };  ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Is there something I'm missing?  Any help is appreciated!  Thank you.


3 replies

Userlevel 4
Badge +12

Hi Angela,

you could try to let SharePoint validate the input. If it fails it will surely prevent data from being saved.

SharePoint Column Validation Examples | Mike Smith's Tech Training Notes SharePoint, PowerShell and .Net! 

Best regards

Enrico

Badge +9

Why not create an auxiliary column that contains the contents of both columns concatenated?

Badge +5

I can concatenate the two fields but I can't make a calculated field "unique" as SharePoint doesn't offer that option.

Perhaps I can try using a lookup on the form to check if anything matches with the concatenated field?  I'll give that a go....

Reply