Make Variable type 'Checkbox' mandatory

pstifter
Tera Contributor

I have created a label and added 4 Checkboxes which each must be read and checked prior to providing an electronic signature for a particular Human Resources catalog item request.   Even though the checkbox variables are marked as mandatory, one is not required to check these boxes in order to submit the request.

How does one make these variables each mandatory?

1 ACCEPTED SOLUTION

arnabwa
Giga Guru

Hi Paul,
I was in exactly the similar situation once a while. I used this code and it worked perfectly. Just paste the code & make variable name changes. Observe the magic.



Name:  Mandatory checkboxes
Type:  onSubmit
Applies to:  A Variable Set OR A Catalog Item
Script:



function  onSubmit(){
     //Set the mandatory checkbox variable names and total mandatory count here
     var  mandatoryVars  =  'option1,option2,option3,option4';
     var  mandatoryCount  =  2;
     
     var  passed  =  forceMandatoryCheckboxes(mandatoryVars,  mandatoryCount);
     if(!passed){
         //Abort the submit
         alert('You must select at least '  +  mandatoryCount  +  ' options.');
         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;
}


View solution in original post

19 REPLIES 19

rohantyagi
ServiceNow Employee
ServiceNow Employee

I got your point. For a checkbox variable, "mandatory" does not have any effect. Because, if the checkbox is unchecked the value is False and if it is checked the value is true. So essentially it always has a value. Now this think is in our plan to improve and you may see "mandatory" more effective in future releases.


For now, I suggest using a Yes/No variable and include "None" value. Make it mandatory and put a help text that select "yes" before proceeding (or whatever you want to explain). Now making Yes/No variable mandatory and including "None" will default your variable value to None and give an error message while submitting the order with None value. Either yes or no has to be selected to proceed. Hope that helps


find_real_file.png



find_real_file.png



find_real_file.png


Is the issue that you want the user to check at least one checkbox, maybe more.   But not require the user to check all of them?


If this is your use case, check out this article by Mark Stanger:



Make Checkbox Variables Mandatory - ServiceNow Guru


arnabwa
Giga Guru

Hi Paul,
I was in exactly the similar situation once a while. I used this code and it worked perfectly. Just paste the code & make variable name changes. Observe the magic.



Name:  Mandatory checkboxes
Type:  onSubmit
Applies to:  A Variable Set OR A Catalog Item
Script:



function  onSubmit(){
     //Set the mandatory checkbox variable names and total mandatory count here
     var  mandatoryVars  =  'option1,option2,option3,option4';
     var  mandatoryCount  =  2;
     
     var  passed  =  forceMandatoryCheckboxes(mandatoryVars,  mandatoryCount);
     if(!passed){
         //Abort the submit
         alert('You must select at least '  +  mandatoryCount  +  ' options.');
         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;
}


pstifter
Tera Contributor

Thanks Arnab - This is very close.


The client script does require all 4 boxes be checked or a info message pops up stating to do so.   However, the request does not submit if all fields are properly filled in (including the 4 check boxes).


Forgot to add, request just sits idle.   No error or popup indicating why it failed to submit.