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.

Retrieve the age of oldest ticket that is kept open

Vamshi_ch123
Tera Contributor

Hi All, 

I'm trying the fetch the age of oldest incident ticket that is still open in report

i) I have written a script include and make a call of this script to report but it's giving me the invalid and wrong input.

ii) I have tried this script in background but here it is showing an error 

 

How to fetch the age in days.

var num = [];
 var gr_old = new GlideRecord('incident');
		gr_old.addEncodedQuery('stateNOT IN6,7,8^assignment_group=1c3d4688db3bb380c6e8ba2139961995');
        gr_old.orderBy('sys_created_on');
        gr_old.query();
        if (gr_old.next()) {
            var gdt = new GlideDate();
            gr_old.u_today = gdt;
			gs.info('Oldest @@@ '+gdt);
            num.push(gr_old.getValue('number'));
            gr_old.update();
        }
        return num;
    },

 

Vamshi_ch123_0-1682236361728.png

 

 

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@Vamshi_ch123 Use the following script to get the incident which is the oldest incident and still in the new state.

 

var glideIncident = new GlideRecord('incident');
glideIncident.addQuery('state',1);
glideIncident.orderBy('sys_created_on');
glideIncident.setLimit(1);
glideIncident.query();
if(glideIncident.next()){    
    var incidentDate = new GlideDateTime();
    incidentDate.setValue(glideIncident.sys_created_on);
    var dur = new GlideDuration();
     dur = GlideDateTime.subtract(incidentDate,new GlideDateTime());

    gs.info('Incident '+ glideIncident.number +' has been in the New state since '+dur.getDisplayValue());
}

 

Hi @Sandeep Rajput 

 

I tried executing this script in the background it's giving me the result but when I call this script to report it's giving the age as null 

Vamshi_ch123_0-1682245187083.png

 

@Vamshi_ch123 The script I have shared returns the number and Age of the open incident. If you want only the number then modify the script as follows.

 

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;

Please note that age will not be returned in this case. If you want both then instead of returning single variable, you need to return an object.

 

var glideIncident = new GlideRecord('incident');
glideIncident.addQuery('state',1);
glideIncident.orderBy('sys_created_on');
glideIncident.setLimit(1);
glideIncident.query();
var oldIncident = {};
if(glideIncident.next()){    
    var incidentDate = new GlideDateTime();
    incidentDate.setValue(glideIncident.sys_created_on);
    var dur = new GlideDuration();
     dur = GlideDateTime.subtract(incidentDate,new GlideDateTime());

    oldIncident['number']=glideIncident.number; 
    oldIncident['age']=dur.getDisplayValue();
}
return oldIncident;

 

 

Hi could you help me to show only the age?

 

I want to show in a single score the age of the oldest incident.

 

I really appreciate your help, thanks.