How to prevent duplicate data entries in Multi row variable set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2020 08:01 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2020 08:46 AM
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
Please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2020 10:31 AM
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;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2020 09:49 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2020 06:22 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2020 02:35 AM
parent.g_form is returning undefined for me.