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

Community Alums
Not applicable
(function executeRule(current, previous /*null when async*/) {

	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)

{

current.u_incident_time = 'Below 30 days';

}

if(days < 30 && days <= 60)

{

current.u_incident_time = 'Above 30 days';

}

if(days > 60)

{

current.u_incident_time = 'Above 60 days';

}
})(current, previous);

Hi @Community Alums tested in PDI, works as expected, please find below script,

Your BR should be after insert/update

script:

(function executeRule(current, previous /*null when async*/) {
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 days';
}
if(days < 30 && days <= 60)
{
current.u_incident_time= 'Above 30 days';
}
if(days > 60)
{
current.u_incident_time= 'Above 60 days';
}
current.update();//I missed current.update

})(current, previous);
Result:
HarishKM_0-1709699343325.pngHarishKM_1-1709699567170.png

 

Regards
Harish

Community Alums
Not applicable

Hello @Harish KM 

Not working on me, should i add those choices in the choice list or not?

 

Hi @Community Alums you need to have choices in the field to display , what type of field are you using for Incident time?

Regards
Harish

Community Alums
Not applicable

@Harish KM 

Choice field