Script include which is triggering from scheduled job is giving performance issues
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hey @Anusha Dande ,
You can update the code in the Script Include as follows:
var grInc = new GlideRecord("incident");
grInc.addEncodedQuery("closed_at>=javascript: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
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
