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-27-2020 09:36 AM
I think the problem is that you're using the script inside the mrvs inside of on the form.
g_form.getValue("box_details_for_pick_up") will only return a result in a form script and will return empty in mrvs script.
Within mrvs, g_form.getValue("barcode") will return value of Barcode field that currently being entered onSubmit() in a script within mrvs. However, there doesn't seems like a way to get other rows from script within mrvs.
That is, the form need to have a variable to trigger the onChange condition execute the script to check if there is any duplicate entries. This mean it would not check while the user is adding/modifying rows in the mvrs but when the focus changes to other variable on the form.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2020 09:55 AM
I've tested your code by adding a creating a onSubmit script on the form. If there is a duplicate entry in the mvrs, it would show an alert and stop the submit. (Note, there was no need for another variable on the form.)
Output on submitting a form when there is a duplicate entry in mrvs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2020 10:08 AM
Can you help me with what code did you use to achieve it?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2020 10:14 AM
I just used your code but moved it to script on the form (Catalog Client Script) instead of script in the mrvs.
However, I found a way to get the value within the form. Code below will executes is script within the form but will only check on the next new row.
That is, it would only show error if I add a new row, add a duplicate row, then add another row.
It won't error while adding the 2nd row but on the third.
function onSubmit() {
var value = parent.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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2020 10:32 AM
Once i moved the script from Variable set to catalog item, it worked as per the requirement.
Thanks for the help 🙂