- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2022 01:36 PM
Hello all! I am newer to the scripting side of ServiceNow and I am making a form for users that has 16 checkbox variables available when the user selects a certain answer on a select box in an earlier portion of the form, but I need it so that users must select 3, but can not select more than that. The script I have below will pop up the message and will not allow the form to be submitted when more or less than 3 are submitted, but the issue is it will do the same with 3 submitted as well. How can I fix this? Thank you so much in advance!
function onSubmit(){
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,checkbox16';
var mandatoryCount = 3;
var maxCount = 3;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if(!passed){
//Abort the submit
alert('Please select ' + '3.');
return false;
}
function forceMandatoryCheckboxes(mandatory, count){
//Split the mandatory variable names into an array
mandatory = mandatory.split(',');
var answer = false;
var varFound = false;
var numTrueMan = 3;
var numTrueMax = 3;
var x = 3;
//Check each variable in the array
while(mandatory[x]){
// alert('starting mandatory loop with mandatory being ' + mandatory[x]);
if(g_form.getControl(mandatory[x])){
if(g_form.getValue(mandatory[x]) == 'true'){
numTrueMax ++;
numTrueMan ++;
}
x++;
}
else{
x++;
}
}
}
if(numTrueMax < 4 && numTrueMan > 2){
answer = true;
}
else{
answer = false;
}
return answer;
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-09-2022 01:00 AM
Hi Morgan,
Try the following script.
function onSubmit() {
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,checkbox16';
var mandatoryCount = 3;
var maxCount = 3;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if (!passed) {
//Abort the submit
alert('Please select ' + '3.');
return false;
}
function forceMandatoryCheckboxes(mandatory, count) {
mandatory = mandatory.split(',');
var cnt = 0;
for (var i = 0; i < mandatory.length; i++) {
if (g_form.getValue(mandatory[i]) == 'true') {
cnt++;
if (cnt > count) {
return false;
}
}
}
return (cnt == count);
}
}
Execution:
case 1: nothing checked
case 2: 2 checked
case 3: 5 checked
case 4: 3 checked. Form processed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-08-2022 01:41 PM
UI Policies will help with this. Need a condition script that determines when 3 have been selected then make them all Read Only with the Policy Actions.
Your onSubmit is good for making sure they have selected at least 3. You can also do an onLoad Client Script that displays a message that any 3 are required, then an onChange for each checkbox that checks the count (set a count in a Display Business Rule g_scratchpad.count = 0;), if < 2 add 1 to the g_scratchpad.count, the onChange should add 1 then check the value. Once it reaches 3, clear the message.
Aoife

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-09-2022 01:00 AM
Hi Morgan,
Try the following script.
function onSubmit() {
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,checkbox16';
var mandatoryCount = 3;
var maxCount = 3;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if (!passed) {
//Abort the submit
alert('Please select ' + '3.');
return false;
}
function forceMandatoryCheckboxes(mandatory, count) {
mandatory = mandatory.split(',');
var cnt = 0;
for (var i = 0; i < mandatory.length; i++) {
if (g_form.getValue(mandatory[i]) == 'true') {
cnt++;
if (cnt > count) {
return false;
}
}
}
return (cnt == count);
}
}
Execution:
case 1: nothing checked
case 2: 2 checked
case 3: 5 checked
case 4: 3 checked. Form processed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2022 12:10 PM
Thank you so much Hitoshi! This fixed my issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-09-2022 07:56 PM
Hi,
Try below code, replace the checkbox with your checkbox names.
function onSubmit() {
var checkboxes = ['checkbox1', 'checkbox2', 'checkbox3', 'checkbox4'];
var isSelected = false;
for (var i = 0; i < checkboxes.length; i++) {
if (g_form.getValue(checkboxes[i]) == 'true') {
isSelected = true;
break;
}
}
if (isSelected == false) {
g_form.addErrorMessage('Please select atleast 3 checkboxs');
return false;
}
}
Please mark my response as correct or helpful if applicable