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.

background scripts returning "undefined" value

ramandeep
Kilo Contributor

Friends,

I am running below background script. As shown in output, count is 1 but number returned is "undefined". Please help me in getting the correct number?

var abc = new GlideRecord('change_task');

abc.addQuery('number', 'CTASK0023280');

abc.query();

var count=abc.getRowCount();

//gs.log('Parent is '+ abc.sys_id);

gs.log('count is '+count);

gs.print('Number is ' + abc.number);

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[0:00:00.006] Script completed in scope global: script


*** Script: count is 1
*** Script: Number is undefined
1 ACCEPTED SOLUTION

ramireddy
Mega Guru

you need to iterate the table after querying.



var abc = new GlideRecord('change_task');


abc.addQuery('number', 'CTASK0023280');


abc.query();


var count=abc.getRowCount();


//gs.log('Parent is '+ abc.sys_id);


gs.log('count is '+count);


while(abc.next())


{


gs.print('Number is ' + abc.number);


}


View solution in original post

4 REPLIES 4

ramireddy
Mega Guru

you need to iterate the table after querying.



var abc = new GlideRecord('change_task');


abc.addQuery('number', 'CTASK0023280');


abc.query();


var count=abc.getRowCount();


//gs.log('Parent is '+ abc.sys_id);


gs.log('count is '+count);


while(abc.next())


{


gs.print('Number is ' + abc.number);


}


santoshsahoonis
Kilo Guru

add a line:   abc.next();


var abc = new GlideRecord('change_task');


abc.addQuery('number', 'CTASK0023280');


abc.query();



abc.next();



var count=abc.getRowCount();


//gs.log('Parent is '+ abc.sys_id);


gs.log('count is '+count);


gs.print('Number is ' + abc.number);


Anurag Tripathi
Mega Patron
Mega Patron

you missed abc.next() buddy!!


-Anurag

Midhun1
Giga Guru

Hi,



After var count=abc.getRowCount();


add below:



if(abc.next())


{


gs.log('count is '+count);


gs.print('Number is ' + abc.number);


}