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

Maddysunil
Kilo Sage

@Community Alums 

You can write Business rule which should run on after insert/update, Below is the script you can use it:

 

 

(function executeRule(current, previous) {
    // Calculate incident age based on created date
    var createdDate = new GlideDateTime(current.getValue('sys_created_on'));
    var today = new GlideDateTime();
    var daysDifference = gs.dateDiff(createdDate, today, true); // Get difference in days
    
    // Update incident age choice list field
    if (daysDifference < 30) {
        current.incident_age = 'Below 30 days';
    } else if (daysDifference >= 30 && daysDifference < 60) {
        current.incident_age = 'Above 30 days';
    } else if (daysDifference >= 60 && daysDifference < 90) {
        current.incident_age = 'Above 60 days';
    } else {
        current.incident_age = 'Above 90 days';
    }
})(current, previous);

 

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

Thanks

Community Alums
Not applicable

Hello @Maddysunil 

Not working only showing Above 30 days

@Community Alums 

Seems like you daysDifference is falling between 30 to 60. Did you check on newly created incident you should get Below 30 days . 

 

Community Alums
Not applicable

@Maddysunil 

Should created in the choice the Below 30 days, Above 30 days and so on? if not it will show nothing on the Incident Age field

Hi @Community Alums 

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