Cancelling Existing Incident SLAs and Attaching New Ones

Paul Curwen
Giga Sage

Most of the time when amending existing SLAs you can simply amend the existing SLA with an end date/time Start condition and put a new date/time Start Condition on the new SLA version, but sometimes you may need to completely cancel and attach a set of different SLAs for example when a contract is totally renegotiated. Well this script will do that for you:

 

(function() {
try {
gs.info(‘Starting batch SLA fix process…’);

```
    // Query for incidents that need SLA fix
    var incGR = new GlideRecord('incident');
    incGR.addQuery('active', true);
    incGR.addQuery('state', '!=', 6); // Not closed
    incGR.addQuery('state', '!=', 7); // Not cancelled
    // Add additional query conditions as needed
    // Example: incGR.addQuery('assignment_group', 'specific_group_sys_id');
    incGR.query();
   
    var processedCount = 0;
    var totalApplied = 0;
   
    while (incGR.next()) {
        gs.info('\n--- Processing: ' + incGR.number + ' ---');
       
        // Cancel old SLAs
        var taskSLAGR = new GlideRecord('task_sla');
        taskSLAGR.addQuery('task', incGR.sys_id);
        taskSLAGR.addQuery('active', true);
        taskSLAGR.query();
       
        var cancelledForThis = 0;
        while (taskSLAGR.next()) {
            taskSLAGR.active = false;
            taskSLAGR.stage = 'cancelled';
            taskSLAGR.update();
            cancelledForThis++;
        }
        gs.info('Cancelled ' + cancelledForThis + ' SLA(s)');
       
        // Trigger SLA recalculation using TaskSLAController
        gs.info('Triggering SLA attachment...');
        var slaController = new TaskSLAController();
        slaController.attach(incGR);
       
        gs.sleep(1000); // Brief pause for processing
       
        // Count applied SLAs
        var appliedSLAGR = new GlideRecord('task_sla');
        appliedSLAGR.addQuery('task', incGR.sys_id);
        appliedSLAGR.addQuery('active', true);
        appliedSLAGR.query();
        var appliedForThis = appliedSLAGR.getRowCount();
       
        gs.info('Applied ' + appliedForThis + ' new SLA(s)');
        totalApplied += appliedForThis;
        processedCount++;
    }
   
    gs.info('\n==================================================');
    gs.info('Batch processing complete!');
    gs.info('Processed ' + processedCount + ' incident(s)');
    gs.info('Total SLAs applied: ' + totalApplied);
    gs.info('==================================================');
   
} catch (e) {
    gs.error('Error in batch SLA fix: ' + e.message);
    gs.error('Stack trace: ' + e.stack);
}
```

})();

 

Hope this helps anyone who finds it

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul
0 REPLIES 0