The CreatorCon Call for Content is officially open! Get started here.

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

bernyalvarado
Mega Sage

Hi Neha, you mean by the last created record on the table?



Thanks,


Berny


If so you can do a GlideRecord and do a orderByDesc on the field sys_created_on



Thanks,


Berny


bernyalvarado
Mega Sage

Here goes an example:



var gr = new GlideRecord ('incident');


gr.orderByDesc('sys_created_on');


gr.setLimit(1);


gr.query();


if (gr.next()){


  gs.print (gr.number + ' - ' + gr.sys_created_on);


}


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