How to prevent duplicate data entries in Multi row variable set

Surabhi5
Kilo Contributor

Hi,

I have created a multi-row variable set for one of the catalog items. I want to prevent user from entering duplicate entries.

I referred to https://community.servicenow.com/community?id=community_question&sys_id=8557faef1b8a5050305fea89bd4b...

However it doesn't seem to be doing anything for me. Attached is the image of the variable set i am using.

Below is the script i modified for the same taking reference of the provided script.

Any help would be really appreciated.

function onSubmit() {
//Type appropriate comment here, and begin script below

var value = g_form.getValue('box_details_for_pick_up');

var arr = JSON.parse(value);
var totalLength = arr.length;

var arr2 = [];
for(var i=0;i<arr.length;i++){
if(!searchExisting(arr[i].barcode,arr2)){
arr2.push(arr[i]);
}
}

var finalObjLength = arr2.length;

if(finalObjLength != totalLength){
alert('There are duplicates please remove those before submitting');
return false;
}
}

function searchExisting(barcode,newArray){

for(var i=0;i<newArray.length;i++){
if(newArray[i].barcode == barcode){
return true;
}
}
return false;
}

Regards,

Surabhi

22 REPLIES 22

Hi Surabhi,

Glad to know that it worked.

The client script needs to be catalog client script only as this is for your catalog item.

The same was mentioned in the link below

https://community.servicenow.com/community?id=community_question&sys_id=8557faef1b8a5050305fea89bd4b...

Please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hitoshi Ozawa
Giga Sage
Giga Sage

Got it. This is much better because it works in mrvs script as row is being added to mrvs instead of form submission.

function onSubmit() {

    var value = parent.g_form.getValue("box_details_for_pick_up");
    var arr = JSON.parse(value);

    if (searchExisting(g_form.getValue("barcode"), arr)) {
        alert('There are duplicates please remove those before submitting');
		return false;
    }
}

function searchExisting(barcode, newArray) {

    for (var i = 0; i < newArray.length; i++) {
        if (newArray[i].barcode == barcode) {
            return true;
        }
    }
    return false;
}

Script you've posted works only on the script on the form while the script I've provided above would work with the mrvs script to show error when the row is attempted to be added to mrvs.

Found a problem with my above script. It also prevents row from being edited because the row already exists. Will try to find a way to resolve this issue.

parent.g_form is returning undefined for me.