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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Surabhi,

Did you try adding alert and check?

is the variable name proper?

So you have multiple variables and want to check for only 1 key i.e. barcode?

Regards
Ankur

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

Yes, i want to put a validation on only 1 variable called "barcode".

The name of the variable set is "box_details_for_pick_up".

The variable on which i want the validation to be done is "barcode".

For some reason it just lets me submit the form and doesnt give any error for the duplicate entries.

Willem
Giga Sage
Giga Sage

Your script is running on Submit, that will only check when you submit the complete Item. So if you submit the item, you do not get the alert?

Surabhi5
Kilo Contributor

Yes after i have added duplicate values, when i go to submit the request, it lets me submit it.

Not sure what am i doing wrong here.