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.

query in script

Srini19
Tera Contributor

Hi Every one,

Just want to know why I am getting result of this script like this. I am just trying to do this against problem table via background script. Kindly advise.

Script:

var pr=new GlideAggregate('problem');
pr.addAggregate('COUNT');
pr.groupBy('parent.number');
pr.query();
while(pr.next())
{
    gs.print(+pr.getDisplayValue('parent.number')+" "+pr.getAggregate('COUNT'));
}

 

 

output:

 

*** Script: 0 52
*** Script: NaN 1
*** Script: NaN 28
*** Script: NaN 1

 

1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

Remove that '+' sign. That is trying to make a number of your 'number' field, while that's a string (PRB0012345). Since it's not a number, it sets NaN (Not a Number).


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

3 REPLIES 3

Mark Manders
Mega Patron

Remove that '+' sign. That is trying to make a number of your 'number' field, while that's a string (PRB0012345). Since it's not a number, it sets NaN (Not a Number).


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Pranesh072
Mega Sage
var pr=new GlideAggregate('problem');
pr.addAggregate('COUNT');
pr.groupBy('parent.number');
pr.query();
while(pr.next())
{
    gs.print(pr.getDisplayValue('parent.number')+"  :  "+pr.getAggregate('COUNT'));
}

Ankur Bawiskar
Tera Patron
Tera Patron

@Srini19 

try this line

-> the + operator is unary plus operator in javascript

-> it is trying to convert the value to a number

-> if value is not a number then it's giving NaN or 0 (if value is empty or null)

gs.print(pr.getDisplayValue('parent.number') + " " + pr.getAggregate('COUNT'));

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader