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.

How to query for the latest record in a table?

nehayadav
Kilo Contributor

Hello,

How can I query for the latest record from a table.

thanks!

1 ACCEPTED SOLUTION

Please note that the setLimit(1) will limit the query to only the first record returned by the query and the orderByDesc('sys_created_on') will sort the results based on its created date been the most recent one the first one in the list.



I hope this helps



Thanks,


Berny


View solution in original post

10 REPLIES 10

Community Alums
Not applicable

Hi Hari,

 

We can use setLimit(10) to get latest 10 records.

 

var gr=new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
while(gr.next()){
gs.info(gr.number);
}