Catalog Item select box, value of -- None -- ???

will_smith
Mega Guru

Below is my script for a quick test catalog item. I am trying to stop the catalog item from submitting if the Default App is set to "-- None --", but I can't figure out how to check for that value. can anyone help me figure out what value -- None -- holds in a select box? Is it null, undefined, or empty? And, aren't all of those options picked up by .nil?

For the following snippet, I have tried the following combinations to get app to be checked. None of these are working for me.

- (!app)

- (app.nil())

- JSUtils.nil(app)

//Only if the company is empty and they checked more than value of count the order is cancelled. otherwise pass it through

if ((!app) && (numTrue > count || numTrue === 0)) {

answer = false;

} else {

answer = true;

}

////////// FULL SCRIPT BELOW \\\\\\\\\\\\\\\\\\\

//Client Requirement: If they pick more than 1, we want to know which one is their default. And we'll have them type it in, if they haven't already. If default company is filled in - then proceed.

function onSubmit(){

//Set the mandatory checkbox variable names and total mandatory count here

var mandatoryVars = 'reader,lotus,ppt,visio';

var mandatoryCount = 1;

var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);

if(!passed){

//Abort the submit

g_form.showFieldMsg('app_default','Please type in your default company.','error');

field='app_default';

var fieldToFocus = g_form.getControl(field);

fieldToFocus.focus();

return false;

}

}

function forceMandatoryCheckboxes(mandatory, count){

//Split the mandatory variable names into an array

mandatory = mandatory.split(',');

var answer = false;

var varFound = false;

var numTrue = 0;

var app = g_form.getValue('app_default');

//Check each variable in the array

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

//Check to see if variable exists

if(g_form.getControl(mandatory[x])){

varFound = true;

//Check to see if variable is set to 'true'

if(g_form.getValue(mandatory[x]) == 'true'){

numTrue ++;

//Exit the loop if we have reached required number of 'true'

if(numTrue > count){

answer = true;

break;

}

}

}

}

alert('You selected ' + numTrue + ' options.');

alert('You chose a default company of ' + app);

//Only if the company is empty and they checked more than value of count the order is cancelled. otherwise pass it through

if ((!app) && (numTrue > count || numTrue === 0)) {

answer = false;

} else {

answer = true;

}

alert('Final answer is ' + answer);

return answer;

}

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi William,



None for select box will hold the blank value. Sample script


var state = g_form.getValue('state'); //assuming state is the name of the catalog variable


alert(state);


if(state == '')


{


alert('1');


}


View solution in original post

10 REPLIES 10

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi William,



None for select box will hold the blank value. Sample script


var state = g_form.getValue('state'); //assuming state is the name of the catalog variable


alert(state);


if(state == '')


{


alert('1');


}


You can however configure the dictionary and not include "none" and then create your own choice "none" by giving the lowest order number, in case you want the value to be "none" at script level.


Please make sure if you do that then set the default value to "none" in dictionary of catalog variable


srinivasthelu
Tera Guru

Hi William,



I would execute below line from console first to see the exact value of the Label '-None-'.



alert(g_form.getValue('fieldName'));



Thanks


Srini


Hi Srini, I'm still somewhat new to the development side of ServiceNow, when you say "from the console" - where exactly would I put this?



I tried the console in Chrome dev tools - it didn't recognize g_form.