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

Let's say you have a onchange client script on your global checkbox.


All you have do after placing the above function in a onload script, is write this in the onchange script.



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


  selectUnselectFields(fieldName,newValue);


Thanks Kalai, I am getting somewhere... slowly!



So now I have a onLoad client script:



function selectUnselectFields(u_member_firm_impacted_global,display)


{


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


  var stringResult='';


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


  {


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


  }


  eval(stringResult);


}




function onLoad() {


  var u_member_firm_impacted_global=["u_member_firm_impacted_nrfa","u_member_firm_impacted_nrfcan","u_member_firm_impacted_nrfllp","u_member_firm_impacted_nrfus","u_member_firm_impacted_nrfsa"];


  selectUnselectFields(u_member_firm_impacted_global,true);


}



AND an onChange client script



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') {


          return;


    }




    //Type appropriate comment here, and begin script below



  var fieldName=["u_member_firm_impacted_nrfa","u_member_firm_impacted_nrfcan","u_member_firm_impacted_nrfllp","u_member_firm_impacted_nrfus","u_member_firm_impacted_nrfsa"];


  selectUnselectFields(fieldName,newValue);


   


}



The select/deselect funtionality of the 'Global' checkbox is working great! However   on update/save of the record the correct choice box values that are checked are not being saved correctly?



By default on a new record all choices except 'Global' are checked. If untick boxes and save, it put back all boxes as checked except 'Global'?



Thanks Kalai!


The code inside onload is not required. Comment it out



function onLoad() {


// var u_member_firm_impacted_global=["u_member_firm_impacted_nrfa","u_member_firm_impacted_nrfcan","u_member_firm_impacted_nrfllp","u_member_firm_impacted_nrfus","u_member_firm_impacted_nrfsa"];


  //selectUnselectFields(u_member_firm_impacted_global,true);


}


Thanks Kalai - this seems to have done the trick! If you get 5 mins if there is anyway you could comment next to the scripts to show me what bit is doing what (especially the array bit) that would be great for my own understanding! Thanks again.


Array bit  contains the names of the fields that you want to check/uncheck.

In the function, you are just looping through the array  and appending all the setValue statements  in a string. Once you have looped through all the field names, you will use eval() to execute all the statement in the string as one shot.  

If needed, you can do a Google search  to know more about how eval() works.