Getting field names from selected check boxes and display them in a text box

jorgetop
Kilo Contributor

Hi all,

Appreciate any help with this case:

I'm trying to get the labels from check boxes selected by user and display them on a multiple text box separated with a coma

I was trying to accomplish this with a run script with no results, I found this in another post(how to print the labels of True/False variables that are true on email templates? )

Not sure if I need an OnChange function or in the run script.

This is the original script from the other post

  1. template.print('<p>Environment Tiers:<br />');  
  2. var fStr2 ='u_environment1,u_environment2'  
  3. var fArr2 = fStr.split(',');  
  4. for (x = 0; x < fArr2.length; x++) {  
  5.       if(current[fArr2[x]] != '') {  
  6.               var label = current[fArr2[x]].getLabel() + '<br />';  
  7.               template.print(label);  
  8.       }  
  9. }  
  10. template.print('</p>');

                  Best Regards

1 ACCEPTED SOLUTION

Based upon your example, it appears that you have a variable called Selected Options that should summarize the option checkboxes that are checked.



Probably the easiest way to accomplish what you are asking for is to use an onSubmit Catalog Client Script. This will check to see what checkboxes you have checked and list them in the Selected Options variable. For sample purposes, I will use the following Variable Questions and Names:


Option 1 [var_opt_1]


Option 2 [var_opt_2]


Option 3 [var_opt_3]


Selected Options [var_opt_selected]



Here is the Catalog Client Script Details:


Name: Set Selected Options onSubmit


Type: onSubmit


UI Type: All


Applies on a Catalog Item view: true


Script:


function onSubmit() {


      var cb1 = (g_form.getValue('var_opt_1') == 'true');


      var cb2 = (g_form.getValue('var_opt_2') == 'true');


      var cb3 = (g_form.getValue('var_opt_3') == 'true');


      var selectedTxt = '';


      if (cb1) {


              selectedTxt += 'Option 1\n';


      }


      if (cb2) {


              selectedTxt += 'Option 2\n';


      }


      if (cb3) {


              selectedTxt += 'Option 3\n';


      }


      g_form.setValue('var_opt_selected', selectedTxt);


}



Be sure to change the values of the variables to match whatever you have in your form.


View solution in original post

6 REPLIES 6

Please mark the answer as correct so that others will see how to solution for your requirement.


Geoffrey2
ServiceNow Employee
ServiceNow Employee

It looks like you're using mismatched variable names on lines 2 and 3. var fStr2, then fStr.split(',').


Otherwise this approach works when I test it.



var fStr2 ='u_environment1,u_environment2';


var fArr2 = fStr2.split(',');


var list = [];



for (var x = 0; x < fArr2.length; x++) {


      if (current[fArr2[x]] == true)


              list.push(current[fArr2[x]].getLabel());


}


template.print('<p>Environment Tiers:<br />' + list.join('<br />') + '</p>');