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

HI William,



Yes. I was talking about Chrome dev tools only.



Make sure that you open the Catalog item in a new window. Not in the nav.do. i.e Not in the homepage.



Thanks


Srini


will_smith
Mega Guru

Ahh... I missed the second part on opening in a new window. Thank you.



It appears it comes back as: undefined.



> alert(g_form.getValue('app_default'));


<- undefined


Hi William,



alert too says undefined?. Then your code should work.



Thanks


Srini


will_smith
Mega Guru

Just to make sure I have this right, undefined is the same as empty string (""), right?


Hi William,



undefined:A variable is declared but no values are assigned


var a;



empty String: A variable is declared and has a empty string in it.


var a='';




i have verified your code once again,



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



typeof(app) is an object. So It is evaluating as true in your if condition.




I have explicitly converted it to object by calling toString method



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



And it started working as expected.



If(!app) goes inside now.



Srini


PS: Hit like, Helpful or Correct depending on the impact of the response