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,

it worked well for me even for 1 value checking

can you try adding alert and check

Regards
Ankur

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

Hi @Ankur Bawiskar ,

I am doing it for one value only "Barcode" as i need it for that field only, doesn't do anything.

If i add 2 rows with same values, it lets me submit the request.

I am not that good at scripting, can you tell me where do i need to add the alert?

 

Regards,

Surabhi

Hi Surabhi,

the same script worked well for me.

Ensure you use catalog client script

Ensure you use proper MRVS variable name and the variable inside it

UI Type - ALL

Regards
Ankur

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

Hi Surabhi,

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

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

to remove the duplicate from the MRVS

 

function onSubmit() {

 

var value = g_form.getValue("vt_b2b_application_location"); // MRVS name
var arr = JSON.parse(value);
var totalLength = (arr.length);
alert(totalLength);

 

var arr2 = [];
for (var i = 0; i < arr.length; i++) {
if (!searchExisting(arr[i].select_app_name, arr2)) { // field 1 application name
arr2.push(arr[i]);
}
}

var finalAppLength = arr2.length;
alert(finalAppLength);

var arr3 = [];
for (var j = 0; j < arr.length; j++) {
if (!searchExisting1(arr[j].select_location, arr3)) { // field 2 location
arr3.push(arr[j]);
}
}

var finalLocLength = arr3.length;
alert(finalLocLength);

var finalObjLength = finalAppLength + finalLocLength;

 

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

 

function searchExisting(select_app_name, newArray) {

 

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

 

}

 


function searchExisting1(select_location, newArray) {

 

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

 

}

 

please check this, code. if any question drop me mail on

 

youtextamit@gmail.com