We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Script include which is triggering from scheduled job is giving performance issues

Anusha Dande
Tera Contributor

Hi 
To update the "age of incident" field value (like 0-3 days, 15 days, more than 15 days), the below scheduled job and script include  written but it is giving performance issues due to looping all the incidents on hour basis and populating/caluculating the "age of incident" field. Could you please suggest another approach which should give same functionality with no performance issues

Schedule job running on hourly basis

updateIncidentAge();

function updateIncidentAge(){
    var jobLogGr = new GlideRecord('samp_job_log');
    jobLogGr.initialize();
    jobLogGr.setValue('name', "Age of incident");
    jobLogGr.setValue('status', "in progress");
    jobLogGr.insert();

    try{
        new IncidentAge().process();
        jobLogGr.setValue('status', "completed");
    }
    catch(error){
        jobLogGr.setValue('status', "failed");
    }
    jobLogGr.update();
}
 
Script include :
var IncidentAge = Class.create();
IncidentAge.prototype = {
    initialize: function()
    {
        var currentDate = new GlideDateTime();
        gs.info(currentDate.getDisplayValue());
        var age = 0;

        // Updating incident age
        var gr = new GlideRecord('incident');
        gr.query();
        while (gr.next())
        {
            var resolvedOn = gr.getDisplayValue('resolved_at');
            gs.info('resolved on:' + resolvedOn);
            var gdt = gr.getDisplayValue('sys_created_on');
            gs.info(gdt);
            var stateis = gr.getValue('state');
            gs.info('state:' + stateis);
            if (stateis == 6 || stateis == 7)
            {
                var diffSeconds = gs.dateDiff(gdt, resolvedOn, true);
                //gs.info(diffSeconds);
                var day = diffSeconds / 86400;
                age = parseInt(day);
                gs.info('age in days:' + age);
               
                if (age >= 0 && age <= 3)
                {
                    gr.setValue('u_age_of_incident', '0-3 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else if (age > 3 && age <= 7) {
                    gr.setValue('u_age_of_incident', '4-7 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else if (age > 7 && age <= 15) {
                    gr.setValue('u_age_of_incident', '8-15 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else if (age > 15 && age <= 30) {
                    gr.setValue('u_age_of_incident', '16-30 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else {
                    gr.setValue('u_age_of_incident', 'More than 30 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                }
               

            }
            else if (stateis == 1 || stateis == 2 || stateis == 3 || stateis == 4 || stateis == 5)
            {
                diffSeconds = gs.dateDiff(gdt, currentDate, true);
                //gs.info(diffSeconds);
                day = diffSeconds / 86400;
                age = parseInt(day);
                gs.info('age in days:' + age);

                if (age >= 0 && age <= 3)
                {
                    gr.setValue('u_age_of_incident', '0-3 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else if (age > 3 && age <= 7) {
                    gr.setValue('u_age_of_incident', '4-7 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else if (age > 7 && age <= 15) {
                    gr.setValue('u_age_of_incident', '8-15 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else if (age > 15 && age <= 30) {
                    gr.setValue('u_age_of_incident', '16-30 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                } else {
                    gr.setValue('u_age_of_incident', 'More than 30 days');
                    gs.info(gr.getValue('u_age_of_incident'));
                }
               
            }
            gr.update();
        }

    },

    type: 'IncidentAge'
};
 
Any inputs really appreciated
 
Thank you
 
6 REPLIES 6

Goka Srinu
Tera Contributor

Hey @Anusha Dande ,

 

You can update the code in the Script Include as follows:

var grInc = new GlideRecord("incident");
grInc.addEncodedQuery("closed_at>=javascript&colon;gs.beginningOfLast3Months()^ORclosed_atISEMPTY")
grInc.query();


while (grInc.next()) {
    var gdt = new GlideDateTime()

    var schedule = new GlideSchedule("38f8b6d2c0a801640075da0e39d47696");

    var duration = schedule.duration(grInc.sys_created_on.getGlideObject(), grInc.sys_updated_on.getGlideObject());
    gs.info(duration.getDurationValue());
    grInc.u_incident_age = duration.getDurationValue();
    grInc.update()

}

  Also, make sure the Incident age field type is set to Glide Duration. As suggested by GlideFather, it's better to run the scheduled job during off-hours to avoid any performance issue.

 

AnkaRaoB
Giga Guru

Hi @Anusha Dande ,

 

The performance issue occurs because the scheduled job recalculates and updates all Incident records every hour, which causes unnecessary looping and database writes. Since “Age of Incident” is a derived value, it should not be stored or recalculated using a scheduled job.

Use a Calculated Field

Step 1: Change the u-age_of_incident field on the Incident table to a Calculated (String) field.

Step 2: Add calculation logic to dynamically determine the age based on sys_created_on and resolved_at, returning values such as 0-3 days, 4-7 days, 8-15 days, 16-30 days, or More than 30 days.

With this approach:

  • No scheduled job is required
  • No looping through records occurs
  • No database updates are performed
  • The value is always accurate and calculated at runtime

This is the same pattern used by ServiceNow for other age-based fields and completely avoids performance issues.

If this response helps you solve the issue, please mark it as the Accepted Solution.

Thanks,

AnkaRao