We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

Raghav Sharma24
Giga 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);

 

View solution in original post

jaheerhattiwale
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

Raghav Sharma24
Giga 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);

 

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.

jaheerhattiwale
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