SLA bulk Repair

RakshithS
Tera Contributor

Hi Team,

 

I need to repair 20,000 incident records in bulk. I tried with a fix script, but it is not working as expected. Could someone please assist me with the correct script?

 

// Bulk SLA Repair for Incidents

(function() {
var gr = new GlideRecord('incident');

// Example filter: only active incidents created last month
gr.addEncodedQuery(
"active=true^sys_created_onONLast month@javascript:gs.beginningOfLastMonth()@javascript:gs.endOfLastMonth()" 
);

gr.query();

var count = 0;
while (gr.next()) {
try {
var slaRepair = new SLARepair();
slaRepair.repair(gr.sys_id); 
gs.print("SLA repaired for Incident: " + gr.number);
count++;
} catch (e) {
gs.print("Error repairing SLA for Incident: " + gr.number + " - " + e);
}
}

gs.print("Total SLAs repaired: " + count);
})();

1 REPLY 1

AnkaRaoB
Mega Guru

Hi @RakshithS ,

When repairing or recalculating SLAs for a large number of Incident records (20,000+), the recommended and supported approach in ServiceNow is to use the Task SLA calculation engine instead of deprecated or unsupported APIs.

Using classes like SLARepair often results in incomplete or inconsistent SLA updates.

  1. 1. Use the Supported API: TaskSLACalculator

ServiceNow manages SLAs through the task and task_sla tables.

The supported way to rebuild SLAs is:

var calc = new TaskSLACalculator();

calc.calculate(gr);

This properly recalculates all applicable SLAs for an Incident.

  1. 2. Process Records in Batches (Very Important)

For large volumes, never process everything in one run.

Batching prevents:

Script timeouts

Performance degradation

Node overload

Recommended batch size: 500–1000 records.

  1. 3. Recommended Batch Fix Script (Production-Safe)

Below is a proven script you can use in Fix Scripts or Background Scripts (test first in sub-prod):

(function() {

    var batchSize = 1000;

    var offset = 0;

    var processed = 0;

    while (true) {

        var gr = new GlideRecord('incident');

 

        Update this filter as needed

        gr.addEncodedQuery(

            "active=true^sys_created_onONLast month@javascript:gs.beginningOfLastMonth()@javascript:gs.endOfLastMonth()"

        );

        gr.orderBy('sys_created_on');

        gr.setLimit(batchSize);

        gr.setOffset(offset);

        gr.query();

        if (!gr.hasNext())

            break;

        while (gr.next()) {

            try {

                var calc = new TaskSLACalculator();

                calc.calculate(gr);

                gs.print("Recalculated SLA for: " + gr.number);

                processed++;

            } catch (ex) {

                gs.print("Error on " + gr.number + " : " + ex);

            }

        }

        gs.print("Processed batch. Total so far: " + processed);

        offset += batchSize;

    }

    gs.print("Completed SLA repair. Total records: " + processed);

 

})();

  1. 4. Use Fix Scripts in Production

In Production, always prefer:

System Definition --Fix Scripts

Fix Scripts are:

Audited

One-time execution

Safer for bulk updates

Run Background Scripts only in non-prod or controlled scenarios.

  1. 5. Recalculate Existing SLAs (Optional)

If task_sla records already exist and only the timers are wrong, you can recalculate directly:

sla.recalculate();

Use this when:

SLAs already exist

Breach/timers are incorrect

No need to recreate SLAs

 

If this helps you then mark it as helpful and accept it as solution.