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 interact with an undefined result in a background script?

Jared Wason
Tera Guru

Hi,

I am working on a script that is looking at the RITMs for Server Decommission forms we have completed, and extracting the server names. Some of the RITMs don't have the variable 'server_name' so the result is undefined, however, I don't know how to interact with the undefined result. I tried 'if variable != 'undefined'' and 'if variable != ''" but I still am seeing the undefined result in my output log statements. How do I interact with undefined results?

 

var a = 0;
var b = 0;
var c = 0;
var d = 0;
var tmp = '';
var reqs = [];
var servers = [];
var retired = [];
var active = [];
getREQs(reqs);
getServers(reqs, servers);
//getHardwareStatus(servers, retired, active);

function getREQs(reqs){
	//get all the REQs of closed server decomission requests
	var gr = new GlideRecord('sc_request');
	gr.addQuery('short_descriptionSTARTSWITHserver decommission');
	gr.addQuery('request_state=closed_complete');
	gr.query();
	while(gr.next()){
		//gs.log(gr.number);
		reqs.push(gr.sys_id.getValue());
		a++;
	}
	return reqs;
}

function getServers(reqs, servers){
	//loop through the RITMs and collect the server names
	for(i = 0; i<reqs.length; i++){
		var gr2 = new GlideRecord('sc_req_item');
		gr2.addQuery('request', reqs[i]);
		gr2.query();
		while(gr2.next()){
            b++; 
			//gs.log(gr2.number);
			//gs.log(gr2.variables.server_name);
            if(gr2.variables.server_name != 'undefined'){
                c++;
				servers.push(gr2.variables.server_name);
                gs.log('REQ:' + reqs[i] + ' RITM:' + gr2.number + ' SERVER:' +gr2.variables.server_name);
            }
			//do something if server_name is undefined...		
		}	
	}
}
1 ACCEPTED SOLUTION

Allen Andreas
Tera Patron

Hello,

It's not a string literal of undefined, but pure undefined. So you'd want to use:

 if(gr2.variables.server_name != undefined){

or could flip it and use:

 if(gr2.variables.server_name){

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

3 REPLIES 3

Allen Andreas
Tera Patron

Hello,

It's not a string literal of undefined, but pure undefined. So you'd want to use:

 if(gr2.variables.server_name != undefined){

or could flip it and use:

 if(gr2.variables.server_name){

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Ah I knew it was a simple thing, just wasn't sure what. That worked, thanks!

Community Alums
Not applicable

What I understood from your problem, I tried to find the solution. Please check and run below code.

 

var a = 0;
var b = 0;
var c = 0;
var d = 0;
var tmp = '';
var reqs = [];
var servers = [];
var retired = [];
var active = [];


getREQs(reqs);
getServers(reqs, servers);

function getREQs(reqs){
//get all the REQs of closed server decomission requests
var gr = new GlideRecord('sc_request');
gr.addQuery('short_descriptionSTARTSWITHserver decommission');
gr.query();
while(gr.next()){
gs.log(gr.number);
reqs.push(gr.sys_id.getValue());
a++;
}
return reqs;
}

function getServers(reqs, servers){
//loop through the RITMs and collect the server names
var gr2 = new GlideRecord('sc_req_item');
gr2.addQuery('request', 'IN' , reqs);
gr2.query();
while(gr2.next()){
var itemvVarTask = new GlideRecord('sc_item_variables_task');
itemvVarTask.addQuery('task', gr2.getValue('sys_id'));
itemvVarTask.query();
while(itemvVarTask.next())
{
servers.push(itemvVarTask.getDisplayValue('variable'));
}
gs.log('REQ:' + gr2.request + ' RITM:' + gr2.number + ' SERVER:' +servers);
}
}