How to use one client script, on multiple variables, in a variable set?

ericgilmore
Tera Guru

I have a Variable Set with 6 variables. Each single line text.

scriptVarSet.jpg

I have a simple Catalog Client Script thats supposed to be applied to the variable set, which checks if the values put in the fields are, 1) numbers and, 2) from (0 - 30).

But, I only know how to make it check ONE variable, so I'd have to create this script for the other 5 variables to get it to work properly for each. I don't want to do this.

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

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

          return;

    }

  g_form.hideErrorBox('tlos_place1');

  var partNumber = g_form.getValue('tlos_place1');

  var numbervalid = /^([0-9]{1,2})$/;

  if (!(partNumber.match(numbervalid)) || partNumber > 30) {

  g_form.showErrorBox('tlos_place1','That entry isn\'t in the expected range \(0-30\). Please try again.',false);

  return;

  } else {

  g_form.hideErrorBox('tlos_place1');

  return;

  }

}

How do I get it to work for all the variables in the variable set w/o attaching the script to each individual variable manually?

1 ACCEPTED SOLUTION

ericgilmore
Tera Guru

OK,



Since I had to attach a onChange client script to each variable in the set anyway, I just went with:



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


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


          return;


  }


      var num = isNaN(newValue);


      if (num || newValue > 30) {


              g_form.showFieldMsg('place1','That entry isn\'t in the expected range \(0-30\). Please try again.','error');


      } else {


              g_form.hideFieldMsg('place1', true);


      }


}




I think I might have had to create a global onLoad script for the Catalog Item in order to get it working the other way. Unfortunately, I don't have access to do that yet.



So rlatorre pretty much had my answer this time.



Thank you all!


View solution in original post

10 REPLIES 10

Daniel Oderbolz
Kilo Sage

In my opinion, Service Now should make it possible to have a list of fields in an onChange client script.

This would create a much cleaner codebase. The approach described here however, works very well.

Thanks!


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel