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
 
3 REPLIES 3

GlideFather
Tera Patron

Hi @Anusha Dande,

 

I see you have there gr.update(), if you weren't updating the records, I would recommend to go with GlideAggregate which is optimal for counting the records without making any changes to them, while GlideRecord is not good for hourly execution. But if you want to know the hourly number of records (what is it good for???) then GlideAggregate shall be doable.

 

Also, if you want to see only the number of records, why don't you create a report/dashboard? 

 

Please explain the motivation why do oyu need to check incidents hourly, is it some micro-manager stepping on agent's neck? Or what's the purpose? 😛

 

EDIT: another thing is the trigger, you can minimise the amount by proper condition like created in last 30 days and only active ones, not checking 2 years old incidents being already closed...

_____
No AI was used in the writing of this post. Pure #GlideFather only

Hi Patron,

 

Thank you for the response.

 

The purpose of the script is to auto populate the value of "age of incident"(string) field irrespective of its state and this script is configured long back and deactivated as per the suggestion of servicenow support team due to performance issues. However, need replacement script which has to give same functionality.

Could you please help me with alternative script if possible

 

Thank you 

Hi @Anusha Dande,

 

in that case I would suggest to execute it early morning or late night hours and to execute it just once or twice a day. 

Having such a script executed hourly is not good idea or as I said above it must be well triggered to check and update only relevant incidents..

_____
No AI was used in the writing of this post. Pure #GlideFather only