I need to create a field to calculate an age of incident

MR Carvalho
Tera Contributor

Hi guys,

 

I need to create a field to calculate an age of incident, but I want to calculate the age with possibilities:

 

If the resolved field is not empty, so make the calculation between {sys_created_on - resolved_at} 

 

OR

 

If the resolved field is empty, so make the calculation between {sys_created_on - date now}.

 

Please, could someone help me with the code to populate my new field?

 

Thanks in advance.

 

1 ACCEPTED SOLUTION

Hi @MR Carvalho check the below script 

var gr = new GlideRecord("incident");
gr.addQuery('number', 'INC0010103'); // Replace with the actual incident number
gr.query();

if (gr.next()) {
var startDateTime = new GlideDateTime(gr.sys_created_on);
if(gr.resolved_at==''){
var endDateTime = new GlideDateTime();
}
else{
var endDateTime = new GlideDateTime(gr.resolved_at);
}

var duration = GlideDateTime.subtract(startDateTime, endDateTime);
var durationInMilliseconds = duration.getNumericValue();
var days = Math.floor(durationInMilliseconds / (1000 * 60 * 60 * 24));
var hours = Math.floor(durationInMilliseconds / (1000 * 60 * 60));
var minutes = Math.floor((durationInMilliseconds % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((durationInMilliseconds % (1000 * 60)) / 1000);

gs.info("Time Difference: " + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds");

}

 

View solution in original post

6 REPLIES 6

Hi @MR Carvalho check the below script 

var gr = new GlideRecord("incident");
gr.addQuery('number', 'INC0010103'); // Replace with the actual incident number
gr.query();

if (gr.next()) {
var startDateTime = new GlideDateTime(gr.sys_created_on);
if(gr.resolved_at==''){
var endDateTime = new GlideDateTime();
}
else{
var endDateTime = new GlideDateTime(gr.resolved_at);
}

var duration = GlideDateTime.subtract(startDateTime, endDateTime);
var durationInMilliseconds = duration.getNumericValue();
var days = Math.floor(durationInMilliseconds / (1000 * 60 * 60 * 24));
var hours = Math.floor(durationInMilliseconds / (1000 * 60 * 60));
var minutes = Math.floor((durationInMilliseconds % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((durationInMilliseconds % (1000 * 60)) / 1000);

gs.info("Time Difference: " + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds");

}

 

@Eswar Chappa many thanks.