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

Get the oldest incident

Arely
Tera Contributor

Hi all,

 

I'm trying to obtain the oldest ticket to show the age in a single score in a dashboard

 

This is the script that I have:

 

function getOldIncident (){
var glideIncident = new GlideRecord('incident');
glideIncident.addQuery('state',1);
glideIncident.orderBy('sys_created_on');
glideIncident.setLimit(1);
glideIncident.query();
var number='';
if(glideIncident.next()){    
number=glideIncident.getValue('number');
}

return number;
}
 
and this is how I applied in the report and also the result that I obtained...
Arely_0-1726012992557.png

 

 I already tried with this other script but I didnt obtain any result
function getOldIncidentTest (){
    var glideIncident = new GlideRecord('incident');
    glideIncident.addQuery('state',1);
    glideIncident.orderBy('sys_created_on');
    glideIncident.setLimit(1);
    glideIncident.query();
    var number='';
    if(glideIncident.next()){    
        var incidentDate = new GlideDateTime();
        incidentDate.setValue(glideIncident.sys_created_on);
        var dur = new GlideDuration();
        dur = GlideDateTime.subtract(incidentDate, new GlideDateTime());

    }

    return dur;
}
4 REPLIES 4

Brad Bowman
Kilo Patron

Is the incident shown the correct or incorrect oldest incident in the New state? What are you expecting to see?

Yes I already got the oldest incident but I want to show only the age of the oldest incident in a single score to add in a dashboard.

 

Hi Arely , did you manage to get the solution, I need to implement a similar usecase.
Any help would be appreciated

Please make it correct or helpful if this solves or help you with your issue for other to make use it.

Thanks & Regards,
Vikrant Sharma

Manolis Sgouro1
Tera Contributor

Hello @Arely ,

 

Both of your scripts seem to work fine.

For the second script, "getOldIncidentTest ", I would use at the end "

return dur.getDisplayValue();"
 
What I believe is wrong is the way you are calling the script in the report filter.
Have you tried to define a dynamic filter option (Link) and use it instead?
 
Cheers,
Manolis.