How to get incident age

Community Alums
Not applicable

Hello,

I have a requirement regarding getting the incident aged based on the created date, incident age will appear in the choice list if incident new will appear (Below 30 days) if the incident above 30 days (Above 30 days) and (Above 60 days) until (Above 90 days).

1 ACCEPTED SOLUTION

Hi @Community Alums I amended few changes,

Change your BR trigger to before insert/update and remove current.update from script.

 

Script:

var days;
var createdAt= new GlideDateTime(current.sys_created_on); //get the incident opened_at date
var now = new GlideDateTime(); //get the current date and time
var duration = gs.dateDiff(createdAt, now, true); //calculate the difference
days = parseInt(Math.floor(duration / 86400)); //convert the duration to days
gs.info('Incident age in days: ' + days);
if(days <= 30)
{
current.u_incident_time= 'below_30';
}
if(days > 30 && days <= 60) // there was error here i was using < 30 hence going to this condition made it to > 30
{
current.u_incident_time= 'above_30';
}
if(days > 60)
{
current.u_incident_time= 'above_60';
}
 
HarishKM_0-1709708961640.png

 

Regards
Harish

View solution in original post

27 REPLIES 27

Harish KM
Kilo Patron
Kilo Patron

Hi @Community Alums here is the script which can do, create after insert/update BR

script:

var days;

var createdAt= new GlideDateTime(current.sys_created_on); //get the incident opened_at date
var now = new GlideDateTime(); //get the current date and time
var duration = gs.dateDiff(createdAt, now, true); //calculate the difference
days = Math.floor(duration / 86400); //convert the duration to days
gs.info('Incident age in days: ' + days);

if(days < 30)

{

//set choice list

}

if(days < 30 and days <= 60)

{

//set choice list

}

if(days > 60)

{

//set choice list

}

 

Regards
Harish

Hi @Community Alums Have you tried the script I shared? The gs.dateDiff will return in duration, you need to convert that to days as below

var duration = gs.dateDiff(createdAt, now, true); //calculate the difference
gs.info(duration );
var days = Math.floor(duration / 86400); //convert the duration to days
gs.info('Incident age in days: ' + days);

Regards
Harish

Community Alums
Not applicable

Hello @Harish KM 

I've tried your script showing nothing

Hi @Community Alums Kindly share your updated script, I can help to fix

Regards
Harish