compare "sys_updated_on" dates and get the most recent updated record

chidanandadhath
Kilo Guru

I have below background script, I want to get the record which is most recently updated and display

var inc=new GlideRecord('incident');
inc.query()
while(inc.next())
{
var updated=inc.sys_updated_on;
var inc1=new GlideRecord('incident');
inc1.query()
while(inc1.next())
{
var updated1=inc.sys_updated_on;
if(updated1>updated)
{
var updated3=updated1;
}
}
gs.print(updated3);
}
}

1 ACCEPTED SOLUTION

andrew_venables
ServiceNow Employee
ServiceNow Employee

Here you go:

var inc=new GlideRecord('incident'); 
inc.orderByDesc('sys_updated_on'); 
inc.setLimit(1); 
inc.query();
while(inc.next()) { 
  gs.info(inc.getDisplayValue()); 
}

View solution in original post

1 REPLY 1

andrew_venables
ServiceNow Employee
ServiceNow Employee

Here you go:

var inc=new GlideRecord('incident'); 
inc.orderByDesc('sys_updated_on'); 
inc.setLimit(1); 
inc.query();
while(inc.next()) { 
  gs.info(inc.getDisplayValue()); 
}