Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

23 REPLIES 23

parent.g_form is returning undefined for me.

Seems parent.g_form doesn't work in a portal. I'll look into it.

Hi Hitoshi Ozawa,

is it possible to check all variables present in MRVS for duplicate instead of single variable?
 
Regards,
Ravi