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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2023 02:28 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2023 06:15 PM
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.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2023 06:17 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader