Help with client script to select/deselect all check boxes

jas101
Tera Expert

Good evening all.

One of our field values 'Domain' has the option of 'Cross-domain' - if this value is chosen various checkboxes with different values appear. One of these check-box options is 'Global' - which when checked means all options (domains) are checked automatically. This is done via an onChange client script which is working fine:

function onChange(control, oldValue, newValue) {

    var global = g_form.getValue('u_member_firm_impacted_global');

      if (global == 'true') {

  g_form.setValue('u_member_firm_impacted_nrfa', 'true');

        g_form.setValue('u_member_firm_impacted_nrfcan', 'true');

        g_form.setValue('u_member_firm_impacted_nrfllp', 'true');

        g_form.setValue('u_member_firm_impacted_nrfsa', 'true');

      g_form.setValue('u_member_firm_impacted_nrfus', 'true');

      }}

There is currently another onChange another client script which exists to deselect the checkboxes if 'Global' is unchecked:

function onChange(control, oldValue, newValue) {

    var global = g_form.getValue('u_member_firm_impacted_global');

      if (global == 'false') {

  g_form.setValue('u_member_firm_impacted_nrfa', 'false');

        g_form.setValue('u_member_firm_impacted_nrfcan', 'false');

        g_form.setValue('u_member_firm_impacted_nrfllp', 'false');

        g_form.setValue('u_member_firm_impacted_nrfsa', 'false');

        g_form.setValue('u_member_firm_impacted_nrfus', 'false');

}}

Again this works in the unselect behaviour but the issue is that if 'Global' is not the checkbox checked and any others are e.g. NRFA and NRFCAN then on update of the record, these values are not saved as of course the above script is saying to blank out all values if 'Global' is false.

Screen Shot 2015-11-05 at 22.04.38.png

Any help on how best to solve this problem sincerely appreciated. I can obviously make the above client script inactive but it would be nice to have the option if Global is unticked to deselect all the checkboxes.

On a different note, if anyone knows how I can reduce the height of the 'Please select all regions....' message that would be a lovely bonus!

Thank-you!

Daniel

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

I would create a function as below for the simplicity of code maintenance. If you want to change fields in future, you will just need to edit the array and the code will still continue to work



function selectUnselectFields(fieldName,display)


{


  var fieldsArray=fieldName.toString().split(',');


  var stringResult='';


  for(var i=0;i<fieldsArray.length;i++)


  {


  stringResult += "g_form.setValue(\""+ fieldsArray[i] + "\","+display+");";


  }


  eval(stringResult);


}




function onLoad() {


  var fieldName=["u_check_1","u_check_2","u_check_3"];


  selectUnselectFields(fieldName,true);


}



Once the function is defined, you can create a onchange client script and call the above function with the field names and a parameter to denote if the checkboxes need to be selected or unselected.


View solution in original post

20 REPLIES 20

Victor Ruiz
Tera Guru

You can probably consolidate the scripts and use a switch statement, check out this blog by: sabell2012


Community Code Snippets - Javascript Switch Statement


Steven Young
Tera Guru

I ran into this a while back.   i have a script that sets everything to True if one box is checked.   Then i had another one that if the box is unchecked to clear the values.



i added a confirm condition in the on change.   so if the global is changed and the value is "false"   confirm,   "do you want to clear all of the check boxes?"   if OK is clicked, proceed to clear the boxes.   if cancel is selected it doesn't do anything except uncheck the "global"



as long as you dont have a Business rule that clears the fields, this should work.



This script is completely untested, but you could do something like this:   all in 1 script.



function onChange(control, oldValue, newValue) {


  var global = g_form.getValue('u_member_firm_impacted_global');


  if (global == 'true') {


  g_form.setValue('u_member_firm_impacted_nrfa', 'true');


  g_form.setValue('u_member_firm_impacted_nrfcan', 'true');


  g_form.setValue('u_member_firm_impacted_nrfllp', 'true');


  g_form.setValue('u_member_firm_impacted_nrfsa', 'true');


  g_form.setValue('u_member_firm_impacted_nrfus', 'true');


  }




  if (global == 'false') {


  var answer = confirm("Would you like to uncheck all of the fields?");


  if (answer==true)


  {


  g_form.setValue('u_member_firm_impacted_nrfa', 'false');


  g_form.setValue('u_member_firm_impacted_nrfcan', 'false');


  g_form.setValue('u_member_firm_impacted_nrfllp', 'false');


  g_form.setValue('u_member_firm_impacted_nrfsa', 'false');


  g_form.setValue('u_member_firm_impacted_nrfus', 'false');


  }


  else


  {


  return false;


  }


  }


}


onChange script shouldn't be triggered on Submit of the form , there will some other reason for this issue.


You could also use a single onchange script on field u_member_firm_impacted_global to achieve this.



function onChange(control, oldValue, newValue){  


  if (newValue == true) {  


  g_form.setValue('u_member_firm_impacted_nrfa', true);  


  g_form.setValue('u_member_firm_impacted_nrfcan', true);  


  g_form.setValue('u_member_firm_impacted_nrfllp', true);  


  g_form.setValue('u_member_firm_impacted_nrfsa', true);  


  g_form.setValue('u_member_firm_impacted_nrfus', true);  


  }


  else{


    g_form.setValue('u_member_firm_impacted_nrfa', false);  


    g_form.setValue('u_member_firm_impacted_nrfcan', false);  


    g_form.setValue('u_member_firm_impacted_nrfllp', false);  


    g_form.setValue('u_member_firm_impacted_nrfsa', false);  


    g_form.setValue('u_member_firm_impacted_nrfus', false);  


  }


  }


Thanks for the input Gurpeet, but using the above script based on field 'GLOBAL (Selects all)' the 'Global' checkbox is no longer selecting all other options when ticked?