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 stop duplicates entering in MRVS based on MRVS Variables(Applies Catalog item/ServicePortal)

shaik11
Tera Expert

Hi,

MRVS consists of 4 variables.

Requirement is to stop the duplicates entering in MRVS based on 4 variables present in variable set. When I applied script for 2 variables it is working correctly. I had written onSubmit Client Script on a Variable Set.

But when I applied the same code and same functionality for 4 variables it is not working. How to apply the duplicates functionality for all variables present in MRVS.

Please provide inputs as Priority. 

Code: 

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 provide required code for the above requirement.

 

Thanks,

Reshma

2 ACCEPTED SOLUTIONS

Sebastian L
Mega Sage

Hi, 

Since I don't know your variables, I have input some test values, but change accordingly and it should work. 

function onSubmit() {
    var multiRow = JSON.parse(parent.g_form.getValue("your_variable_set_here")); //This is the multirow that has already been submitted

    var obj = {
        'test1': g_form.getValue('test1'), //replace test1 with your variable1
        'test2': g_form.getValue('test2'), //replace test1 with your variable2
        'test3': g_form.getValue('test3'), //replace test1 with your variable3
        'test4': g_form.getValue('test4'), //replace test1 with your variable4
    };
	
if(multiRow) { //No need to run this if we don't have any values yet. 
var result = multiRow.filter(function(item) {
//Replace below (test1 etc) with your appropriate variable names!
  return (item.test1 == obj.test1 && item.test2 == obj.test2 && item.test3 == obj.test3 && item.test4 == obj.test4);
});
	if(result.length > 0) {
		g_form.addErrorMessage('You have entered the same twice');
		return false;
	}
}
}

 Please mark as correct if it is working. 🙂

 


Best regards,
Sebastian Laursen

View solution in original post

Thanks @Sebastian L 

The above script is working as expected.

MRVS consists of start day, start time, stop day, stop time variables. All variables type is select box 

Start day choices: monday,tuesday,wednesday,thursday,friday,saturday,sunday

start time choices: ( 00:00,01:00,02:00,03:00,......... 21:00,22:00,23:00) hours time formate

Stop day choices: monday,tuesday,wednesday,thursday,friday,saturday,sunday

stop time choices: ( 00:00,01:00,02:00,03:00,......... 21:00,22:00,23:00) hours time formate

If I enter 1st row as (start_day: monday, start _time:04:00,stop_day: wednesday, stop_time: 10:00)

if I enter 2nd row as (start_day: monday, start _time:06:00,stop_day: wednesday, stop_time: 10:00)

If the times are in between the days which are given in above row ( It needs to  show error message )

 How to achieve the above use case and as well as for remaining days.

 

Regards,

Reshma.

View solution in original post

9 REPLIES 9

Mohith Devatte
Tera Sage
Tera Sage

hello @shaik11 try this 

function onSubmit() {
    var mv = parent.g_form.getValue("your_mvrs_internal_name"); 
    var parsed = JSON.parse(mv);

    if (dups(g_form.getValue("your_column_name_to_Check_duplicate"),parsed)) {
       alert('There are duplicates');
       return false;
    }
}

function dups(key,arr) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].your_column_name_to_Check_duplicate == key) { 
            return true;
        }
    }
    return false;
}

Hope this helps 

 

Mark the answer correct if this helps you

Hi @Mohith Devatte ,

I tried the above code, it is not working when I applied it to 4 Variables present in MRVS.

Here facing issue is when I add the first row with the 4 variable values and when I try to add 2nd row with 3 same variable values and 1 different variable value comparing with 1st row it is showing as duplicate.

Final goal is only when the 4 column variable values are same then only it need to show duplicates rather than that row should be added to MRVS.

How to achieve it

Thanks,

Reshma   

 

@shaik11 so you mean to say if all columns are entered with same value then only it is considered as duplicate ?

@Mohith Devatte  Yes.