how to get the last 10 records in incident table

tom_lundy
Kilo Contributor

I am trying to do a get call to retrieve the latest 10 records in a incident table.

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

Hi Tom,



setLimit(): Sets the limit for how many records you want to fetch.



find_real_file.png



var c=0;


var check= new GlideRecord('incident');


check.addQuery('category','software');


check.setLimit(10);


check.query();


while(check.next())


{


c++


gs.print('check the :'+check.number);


}


gs.print('total nnumber of record is:'+c);



OUTPUT



find_real_file.png



http://wiki.servicenow.com/index.php?title=GlideRecord#setLimit


View solution in original post

8 REPLIES 8

Tim Woodruff
Mega Guru

Hey Tom,



It would be good to see your existing code, and have a better idea of what you're looking for, but I'll take a stab at it.


I'm assuming that by "latest 10 records in the incident table", you're referring to the 10 most recently created records. If that's the case, this code should do ya:



var grIncident = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(10);
gr.query();
while (gr.next()) {


    //do whatever you like, such as push their sys_IDs to an array
}


Hi Timothy,



Good catch - the question inquired how to get the latest incident and to do that sorting descending is required - but sorting on what is an open question.


I kindof missed that until I read your post!



If the reply was informational, please like, mark as helpful or mark as correct!


Shishir Srivast
Mega Sage

you can user setLimit() function in you GlideQuery to get the 10 records ordering by sys_created_on



var gr = new GlideRecord('incident');


gr.orderByDesc('sys_created_on');


gr.setLimit(10);


gr.addQuery();


while(gr.next())


{


// Have you code here.


}


Harsh Vardhan
Giga Patron

Hi Tom,



setLimit(): Sets the limit for how many records you want to fetch.



find_real_file.png



var c=0;


var check= new GlideRecord('incident');


check.addQuery('category','software');


check.setLimit(10);


check.query();


while(check.next())


{


c++


gs.print('check the :'+check.number);


}


gs.print('total nnumber of record is:'+c);



OUTPUT



find_real_file.png



http://wiki.servicenow.com/index.php?title=GlideRecord#setLimit