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

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

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.

Glad it work. Please mark the answer as correct, as it answered your original question.

 

For your second question you need the same logic, but you need to change it up a bit, as you have "floating" variables to check from..

 

So you could do something like the below, where the logic is to look for if the dates are the same (test1 and 3), and then if either test2 or test4 is the same). Else you can also change the logic to look for if it is larger than test2 and smaller than test3.. So use the same principle, but change to your need. 

//Test1 is your first date, test2 are the time for first date.  Test3 are your last date and test4 is the time.  
return (item.test1 == obj.test1 && item.test3 == obj.test3 && (item.test2 == obj.test2 || item.test4 == obj.test4));

 


Best regards,
Sebastian Laursen

 @Sebastian L 

As per the requirement

1st row: start day: monday, start time: 01:00,stop day: wednesday, stop time: 12:00

In the next upcoming rows I need to restrict any entries between monday and wednesday

(Not in between monday to wednesday)

If I need to add next row it should only accept  start time from wednesday,13:00 to any day after that(Ex: stop day: saturday, stoptime: 16:00hrs) .

Again if I add next row it need to accept start time from saturday: 17:00 hrs.

How to write the condition as per the above Scenario.

 

Regards,

Reshma

 

its throwing javascript error in your console