how to print latest 5 incidents in an array

hemasree kanduk
Tera Contributor
how to print latest 5 incidents in an array
2 ACCEPTED SOLUTIONS

RaghavSh
Kilo Patron

var arr=[];

var inc= new GlideRecord('incident');

inc.orderByDesc('sys_created_on');

inc.setLimit(5);

while(inc.next())

{

arr.push(inc.number.toString());

}

gs.print(arr);

 


Raghav
MVP 2023

View solution in original post

jaheerhattiwale
Mega Sage
Mega Sage

@hemasree kanduk use below code

var inc = new GlideRecord ('incident');

inc.orderByDesc('sys_created_on');

inc.setLimit(5);

inc.query();

 

while (inc.next()){

gs.info(inc.number.toString());

}

 

Please mark as correct answer if this solves your issue

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

4 REPLIES 4

RaghavSh
Kilo Patron

var arr=[];

var inc= new GlideRecord('incident');

inc.orderByDesc('sys_created_on');

inc.setLimit(5);

while(inc.next())

{

arr.push(inc.number.toString());

}

gs.print(arr);

 


Raghav
MVP 2023

hii raghav  thankyou for the response, and the code you have written is not printing the incident numbers in an array. and i didn't understand why you have written this line ...for (var i = 0; i < arr.length; i++)....? instead of that, 

i tried this code without that line 


var arr=[];

var inc= new GlideRecord('incident');

inc.orderByDesc('sys_created_on');

inc.setLimit(5);
inc.query();

while(inc.next())
{
arr.push(inc.number.toString());
//for (var i = 0; i < arr.length; i++)

}

gs.print(arr);

 

this code is working fine and returning the latest 5 incidents in an array.

@hemasree kanduk i thought i passed object in array so i tried a for loop, but in this case gs.print(arr); will work fine, nice catch.

I updated the post accordingly.


Raghav
MVP 2023

jaheerhattiwale
Mega Sage
Mega Sage

@hemasree kanduk use below code

var inc = new GlideRecord ('incident');

inc.orderByDesc('sys_created_on');

inc.setLimit(5);

inc.query();

 

while (inc.next()){

gs.info(inc.number.toString());

}

 

Please mark as correct answer if this solves your issue

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023