To calculate age of an incident i have tried below process it is working fine.

Ranjith95
Tera Contributor

To calculate age of an incident even if it is not in resolved state, we can create a field "incident age" of duration type and  calculate using the below code.

 

step 1: Create a field "incident age" of Duration type

step 2: open the dictionary of the field and apply below code in "calculated value" tab

 

(function calculatedFieldValue(current) {

    // Add your code here
    var sd = new GlideDateTime(current.getValue('sys_created_on'));
    var ed = new GlideDateTime();
    var dur = GlideDateTime.subtract(sd,ed);
    return dur;
    //return dur.getDayPart();
    // return the calculated value

})(current);

Ranjith95_0-1704235417339.pngRanjith95_1-1704235518923.png

#servicenowcommunity #incidentage

 

Please mark helpful if it is useful.

3 REPLIES 3

Pratiksha2
Mega Sage

Hello @Ranjith95 ,

Please try below code:

var grIncident = new GlideRecord('incident');

if (grIncident.get('552c48888c033300964f4932b03eb092')) {

    var checkDate1 = grIncident.getValue('opened_at');  // update your field name here as - gr.opened_at

var checkDate2 = new GlideDateTime(checkDate1);

var gdt2 = new GlideDateTime();   // returns current date and time

var statusDate = checkDate2.getDate();

var currentDate = gdt2.getDate();

var diff = new GlideDateTime.subtract(statusDate, currentDate);

var days = diff.getDayPart();

gs.info(days);

}


Mark my correct and helpful, if it is helpful and please hit the thumbs-up button to mark it as the correct solution.
Thanks & Regards,
Pratiksha Kadam

chetanb
Tera Guru

We have some OOTB SLA's field that can be utilize instead of creating new one

Prasanth123
Tera Contributor

Helpful