AnsweredAssumed Answered

Enforce unique entries on multiple columns using JavaScript

Question asked by akalmar on Feb 6, 2018
Latest reply on Feb 7, 2018 by akalmar

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.

Outcomes