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.

Catalog Client Script with multiple criteria for IF/Else

Joe M1
Kilo Contributor

Hi,

New to SeviceNow and trying to figure Catalog Client Scripts to validate and show an error on a form it is not working as expected.  If I use two sets of criteria such as length of the newValue in the field and a specific country from a select box it works ok, but if I add in additional criteria for newValue field such where it does not start with "OC" it does not work properly.  My script is:

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

if (isLoading || newValue == ''){
return;
}
var length=newValue.length;
var eng=newValue.substring(0, 2);
if ((length =6) && (g_form.getValue('u_country_uk') == 'United Kingdom') && (eng != "OC"));
{
alert("invalid characters");
return false;
}
}

 

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Few issues:

1. unless you are testing for a case sensitive "OC", you should force to lower case (or upper, doesn't really matter which one) before doing the compare, otherwise "oc" is not equal to "OC"

2. "length =6" should be "length == 6" to check to see if it is 6 characters long

3. no need to "return false" in an onChange

4. there was a ";" at the end of the if condition that may have broken things as well

Try this out:

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == ''){
    return;
  }
  var length = newValue.length;
  var eng = newValue.substring(0, 2).toLowerCase();  //force to lowercase for compare later on
  if (length == 6 && g_form.getValue('u_country_uk') == 'United Kingdom' && eng != "oc")){
    alert("invalid characters");
  }
}

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Joe,

Try adding a temporary alert on eng right after the var eng line to confirm the value stored in that variable.  It looks like you are returning 3 characters, but only using 2 in your if test.  Since these are all && you don't need the extra parenthesis around the first 2 criteria, but it doesn't hurt anything.  An alternate method to substringing the first 2 characters in this case would be && newValue.indexOf('OC') !=0 meaning it doesn't start with 'OC'.

Joe M1
Kilo Contributor

Hi Brad,

I added the alert after the var eng, but it only returns 2 characters.  I do not understand your remark on returning 3 characters, could you please explain?

Just wanted to confirm eng contained what you were expecting.  Jim's response covers the other 2-3 errors, and one unnecessary line in your script.

Jim Coyne
Kilo Patron

Few issues:

1. unless you are testing for a case sensitive "OC", you should force to lower case (or upper, doesn't really matter which one) before doing the compare, otherwise "oc" is not equal to "OC"

2. "length =6" should be "length == 6" to check to see if it is 6 characters long

3. no need to "return false" in an onChange

4. there was a ";" at the end of the if condition that may have broken things as well

Try this out:

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == ''){
    return;
  }
  var length = newValue.length;
  var eng = newValue.substring(0, 2).toLowerCase();  //force to lowercase for compare later on
  if (length == 6 && g_form.getValue('u_country_uk') == 'United Kingdom' && eng != "oc")){
    alert("invalid characters");
  }
}