Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

I guess I should have said that. This is Helsinki and the Service Portal.



Sorry to be so dense, but you're saying that I have to go anonymous function time on the onLoad?



var checkThisValue = function() {


  function checkParticipants(someVar) {


          // g_form.hideErrorBox(someVar);


      var partNumber = g_form.getValue(someVar);


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



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


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


      return;


      } else {


      g_form.hideErrorBox('someVar');


      return;


      }


  }


}



Or the onChange?


larstange
Mega Sage

In the onLoad define it like this



  1. checkParticipants = function(someVar) {  
  2.           // g_form.hideErrorBox(someVar);  
  3.       var partNumber = g_form.getValue(someVar);  
  4.       var numberValid = /^([0-9]{1,2})$/;  
  5.  
  6.       if (!(partNumber.match(numberValid)) || partNumber > 30) {  
  7.       g_form.showErrorBox('someVar','That entry isn\'t in the expected range \(0-30\). Please try again.',false);  
  8.       return;  
  9.       } else {  
  10.       g_form.hideErrorBox('someVar');  
  11.       return;  
  12.       }  
  13.   }  


Nothing special to do in the onChange


Note that in line 7 and 10 you reference someVar as a string 'someVar' instead


Yes!



For whatever reason, your answer is NOW working in my environment too. Without access to the global UI.



So I have my choice of either now.



Thanks!



larstange


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!