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

Hi @Community Alums in that case you would need to create choices like this

Below 30 days

Above 30 days

Above 60 Days

and need to match the choice value in script like below

current.u_incident_time= 'Above 60 days'; // Above 60 Days is choice value .

Regards
Harish

Community Alums
Not applicable

Hello @Harish KM 

I already created but still not appearing.

 

Hi @Community Alums Can you share your choice value names

and BR condition when to run and advanced script to debug

Regards
Harish

Community Alums
Not applicable

@Harish KM 

Please find the attached images.

Hi @Community Alums ok I found the issue, the choice values are different in my script. I have updated to the correct values as per yours, it should work now. Check bolded lines

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)
{
current.u_incident_time= 'above_30';
}
if(days > 60)
{
current.u_incident_time= 'above_60';
}
current.update();
Regards
Harish