MRVS: Prevent the user from selecting both options in a MRVS?

Edwin Fuller
Tera Guru

Is there a way to prevent the user from selecting both options in a MRVS?

 

I currently have MRVS that contains one field with two choices to select from. The user should only be allowed to select one option either "Single Monitor" or "Dual Monitor"; Screen shot below.

 

Is there a way to implement this logic?

 

 

EdwinFuller_0-1691616277045.png

EdwinFuller_1-1691616520845.png

 

 

2 REPLIES 2

AnveshKumar M
Tera Sage
Tera Sage

Hi @Edwin Fuller 

You mean to say, "if the user selected SingleMonitor in one row, it shouldn't allow dual monitor in any other row"? 

 

You can write an onSubmit client script to do this validation. You can try using the following code in you onSubmit client script.

   

 

function onSubmit(){
var multiRowVariableSet = JSON.parse(g_form.getValue('variable_set_1'));
var val1;
    for (var i = 0; i < multiRowVariableSet.length; i++) {
        if(i === 0){
       val1 = multiRowVariableSet[i].monitor;
}elseif(val1 !=multiRowVariableSet[i].monitor){
alert("You need to choose only one monitor type");
return false;
}
    }​
}

 

 

Replace variable_set_1 to your MRVS internal name.

 

Thanks,
Anvesh

Ankur Bawiskar
Tera Patron
Tera Patron

@Edwin Fuller 

you can do 2 things

1) observer add/remove of that row and do the validation

OR

2) you can use onSubmit catalog client script and check the values are same

onSubmit sample script

function onSubmit(){

	var parsedData = JSON.parse(g_form.getValue('mrvsVariableSetNameHere'));
	var arr = [];

	for(var i=0;i<parsedData.length;i++){
		arr.push(parsedData[i].monitorVariableName.toString());
	}

	var isUnique = checkIfArrayIsUnique(arr);
	if(!isUnique){
		alert("Please select only one option either Single Monitor or Dual Monitor");
		return false;
	}

}

function checkIfArrayIsUnique(arr) {
	var map = {}, i, size;
	for (i = 0, size = arr.length; i < size; i++){
		if (map[arr[i]]){
			return false;
		}
		map[arr[i]] = true;
	}
	return true;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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