The CreatorCon Call for Content is officially open! Get started here.

OnSubmit Client script - Force Checkbox not working from service catalog

KristyS
Kilo Guru

I have an onsubmit client script that will enforce a option of multiple checkboxes that works in the service portal but no longer works in the service catalog.   Can someone help revise this script to enable it to work with the service catalog as well. 

 

function onSubmit()

 {
       //Set the mandatory checkbox variable names and total mandatory count here
       
  var mandatoryVars = 'ipad,token_user,physical_vpn,physical_vpn_deskphone,physical_vpn_no_computer,vendor_token_user,telecom_provider_add_modify_service';
       var mandatoryCount = 1;
    
       
  var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    
   if(!passed)
    
    {
            //Abort the submit
            
     var message = 'You must select at least '
       + mandatoryCount + ' option for Remote Access.';
             g_form.addErrorMessage(message);
             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.hasField(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;

 }

1 ACCEPTED SOLUTION

vinothkumar
Tera Guru

Hi,

 

Can you try the below script.

 

var sTypeofModifys=['checkbox1','checbox2','checkbox3'];

 

            var isSelected=false;

 

            for(var nIndex=0;nIndex<sTypeofModifys.length;nIndex++){

 

                    //alert(g_form.getValue(sTypeofModifys[nIndex]));

 

                    if(g_form.getValue(sTypeofModifys[nIndex])=='true'){

 

                            isSelected=true;

 

                            //alert(isSelected);

 

                            break;

 

                    }

 

            }

 

     

 

      if(isSelected==false){

 

              alert('Please check atleast 1 checkbox');

 

              return false;

 

      }

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kristy,

Did you try to see any error in browser console?

also at what point is it breaking? did you try to add alert

Regards

Ankur

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

The script appears not to be enforcing the mandatory fields because the alert is not presented when an option is not selected.  

If I select an option and click the order button, a request is not submitted.  

 

I have removed all of the variables except for one to see if something was wrong with a variable but this did not resolve.   The client script works if I use it in the portal just not in the backend service catalog.  

vinothkumar
Tera Guru

Hi,

 

Can you try the below script.

 

var sTypeofModifys=['checkbox1','checbox2','checkbox3'];

 

            var isSelected=false;

 

            for(var nIndex=0;nIndex<sTypeofModifys.length;nIndex++){

 

                    //alert(g_form.getValue(sTypeofModifys[nIndex]));

 

                    if(g_form.getValue(sTypeofModifys[nIndex])=='true'){

 

                            isSelected=true;

 

                            //alert(isSelected);

 

                            break;

 

                    }

 

            }

 

     

 

      if(isSelected==false){

 

              alert('Please check atleast 1 checkbox');

 

              return false;

 

      }