Is there a way to group check box variables inside a multi-row variable set to control a mandatory selection for the group?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2021 10:28 AM
Hello,
Is there a way to group checkbox variables inside a multi-row variable set to control a mandatory selection for the group?
This is for a catalog item I have that allows my users to request access to a system. I need to present a list of access roles and the user should be able to select one of more roles by clicking the checkbox. At least one selection is mandatory and one selection should satisfy the mandatory requirement for the group of checkboxes. I am using the checkbox variable for this because it looks like it is the only option from within a MRVS. Using a regular variable set, we have the option to group select boxes under a Label variable to control mandatory selections but Labels are not allowed in MRVS's so trying to figure out how to do this. Any help is greatly appreciated.
Thanks!
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2021 10:56 AM
Catalog Client script on Submit
Get values of all checkboxes- create an if statement and if one of those checkboxes is true allow submit- if all - fase abort the submit and put a message up advising they must select at least one .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2021 12:43 PM
Hi Bammar, thanks for pointing me in the right direction! I just lifted the code below from this post but it's not working, any ideas? I'm bad at scripting. I have it set up as an OnSubmit running on the MRVS.
function onSubmit() {
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'myvariable1,myvariable2';
var mandatoryCount = 1;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if(!passed){
//Abort the submit
alert('You must select at least ' + mandatoryCount + ' role.');
return false;
}
}
function forceMandatoryCheckboxes(mandatory, count){
//Split the mandatory variable names into an array
mandatory = mandatory.split(',');
var answer = false;
var varFound = false;
var numTrue = 0;
//Check each variable in the array
for(x=0;x<mandatory.length;x++){
//Check to see if variable exists
if(g_form.getControl(mandatory[x])){
varFound = true;
//Check to see if variable is set to 'true'
if(g_form.getValue(mandatory[x]) == 'true'){
numTrue ++;
//Exit the loop if we have reached required number of 'true'
if(numTrue >= count){
answer = true;
break;
}
}
}
}
//If we didn't find any of the variables allow the submit
if(varFound == false){
answer = true;
}
//Return true or false
return answer;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2022 01:19 PM
I was searching for an answer to this question myself and came across this post. Here's a work around. You can't use labels in MRVS but you can use a Multiple Choice variable with no question choices to "fake" a label then use a UI policy to make the checkboxes appear to be mandatory. The downside is that the Multiple choice var is it's own column in the grid view but it acts like a label in the form view. Not ideal but does make the checkbox list mandatory while the user is adding the record. See this old post where I got the idea and instructions for the UI policy:
https://community.servicenow.com/community?id=community_blog&sys_id=33ed622ddbd0dbc01dcaf3231f961904