Need the count of checked check boxes in service catalog

sumanth1
Tera Contributor

There are 8 check boxes under one label in  service catalog.

I need to check if 1 check selected or more than 1 check box is selected.

If more than 1 check box is selected I need to display multiple string word in ritm short description.

 

Can any one provide script using array to acheive this.

9 REPLIES 9

Ian Mildon
Tera Guru

Here's an onSubmit Client Script I've used on/off for a long time now:

 

function onSubmit(){
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'u_loc_conf';
    var mandatoryCount = 1;
   
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);

    var iVal = g_form.getValue('priority');
    
    if(!passed && iVal == 1 || !passed && iVal == 2){
        //Abort the submit
        g_form.addErrorMessage("The following mandatory field is not filled in: Location confirmation");
		g_form.flash('u_loc_conf', 'red', 0);
        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;
}

This example only has one checkbox name listed for "mandatoryVars" so just keep adding yours as comma separated values

 

And here is a slight variation on the above script with additional vars

function onSubmit(){
   //Set the mandatory checkbox variable names and total mandatory count here
   
   var mandatoryVars = 'agreement,agree_terms';
   var mandatoryCount = 2;
   
   var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
   if (!passed){
      //Abort the submit
      alert('You must agree to both of the ' + mandatoryCount + ' disclaimer terms.');
      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;
            }
         }
         var getRole = g_form.getValue ('select_role');
	var devicetype = g_form.getValue('comp_device', true);
         
         if (getRole !== 'student'){
            answer = true;
         }
         if (getRole == 'student' && devicetype == 'true'){
            answer = false;
         }
      }
   }
   //If we didn't find any of the variables allow the submit
   if(varFound == false){
      answer = true;
   }
   //Return true or false
   return answer;
}

DUGGI
Giga Guru

@sumanth1 

 

  1. Create a new variable named checkbox_count of type Hidden in your catalog item. This variable will be used to store the number of selected checkboxes.

  2. Create 8 Checkbox variables under the checkbox_container. For example, checkbox1, checkbox2, ..., checkbox8.

  3. Create a new Client Script for your catalog item:

    Name: Count Selected Checkboxes Type: OnChange UI Type: Both Execution Order: 100

    In the script, add the following code:

javascriptCopy code
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    // Replace the following variable names with the actual names of your checkboxes
    var checkboxVars = ['checkbox1', 'checkbox2', 'checkbox3', 'checkbox4', 'checkbox5', 'checkbox6', 'checkbox7', 'checkbox8'];

    var selectedCount = 0;
    for (var i = 0; i < checkboxVars.length; i++) {
        if (g_form.getValue(checkboxVars[i]) === 'true') {
            selectedCount++;
        }
    }

    g_form.setValue('checkbox_count', selectedCount);
}

  1. Create a new Business Rule for your catalog item:

    Name: Update RITM Short Description Table: Requested Item [sc_req_item] When: Before Insert: True Update: True

    In the script, add the following code:

javascriptCopy code
(function executeRule(current, previous /*null when async*/) {
    if (current.cat_item.nil()) {
        return;
    }

    // Replace with your catalog item sys_id
    var catalogItemSysId = 'your_catalog_item_sys_id';

    if (current.cat_item.sys_id == catalogItemSysId) {
        var checkboxCount = parseInt(current.variables.checkbox_count, 10);

        if (checkboxCount > 1) {
            // Replace 'your_string' with the desired string value
            current.short_description = 'Multiple options selected: ' + checkboxCount + ' - your_string';
        } else if (checkboxCount === 1) {
            current.short_description = 'Single option selected';
        } else {
            current.short_description = 'No options selected';
        }
    }
})(current, previous);

Make sure to replace the placeholder variables and sys_id with the actual values in your instance. After implementing these changes, your Service Catalog item should now be able to update the RITM short description based on the number of selected checkboxes.

sumanth1
Tera Contributor

Hi Duggi,

 

If I want to acheive this by writing script in run script activity in workflow .what script should be written.