SLA not getting attached when old one is cancelled
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2025 03:25 AM
I am using following script and when the script executes it should cancel the existing SLA and new sla gets attached as starting condition meets. The script works find when the new sla is different than the old one that is if new sla is 5 days and current sla is 2 days sla then script executes perfectly fine that is it cancel the 2 days sla and 5 days sla get attached successfully. But when new sla is same as current one then instead of cancelling the current sla, it keeps it in progress state. My requirement is: Despite of SLA defination, the current sla must get cancel and new sla should attach.
//Working on task table
var oldSLA = new GlideRecord('task_sla');
//SLA is on this task
oldSLA.addQuery('task.sys_id', current.sys_id);
//Task Type matches this task
oldSLA.addQuery('task.sys_class_name', current.sys_class_name);
oldSLA.query();
while (oldSLA.next())
{
if (oldSLA.stage != 'cancelled') {
slaUtil.cancelSLA(oldSLA);
oldSLA.update();
}
//gs.addInfoMessage('Found SLA for Task: ' + current.number);
gs.addInfoMessage('Please note the new SLA for '+ current.number + '. The caller will be notified of the revised due date');
}
//Raise SLA reset event for notification
gs.eventQueue("finance.sla.reset", current, current.assigned_to.getDisplayValue() , previous.assigned_to.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2025 03:32 AM
To forcefully cancel the current SLA and reattach a new instance of the same SLA, you need to:
- Cancel the existing SLA instance, even if it's from the same definition.
- Ensure the SLA condition is re-evaluated so that a new instance is created.
Here’s how you can modify your script to achieve this:
var slaUtil = new SLAUtils(); // Ensure SLAUtils is available
var oldSLA = new GlideRecord('task_sla');
oldSLA.addQuery('task', current.sys_id);
oldSLA.addQuery('task.sys_class_name', current.sys_class_name);
oldSLA.query();
while (oldSLA.next()) {
if (oldSLA.stage != 'cancelled') {
// Cancel the current SLA
slaUtil.cancelSLA(oldSLA);
oldSLA.update();
gs.addInfoMessage('Cancelled SLA: ' + oldSLA.sla.name);
}
}
// Trigger re-evaluation of SLAs
current.autoSysFields(false); // Prevents system fields from being updated
current.setWorkflow(false); // Prevents business rules from being triggered again
current.update(); // Triggers SLA engine to re-evaluate
gs.eventQueue("finance.sla.reset", current, current.assigned_to.getDisplayValue(), previous.assigned_to.getDisplayValue());
gs.addInfoMessage('New SLA will be attached for task: ' + current.number);
- current.update() after cancelling ensures the SLA engine re-evaluates the task and attaches a new SLA—even if it's the same definition.
- autoSysFields(false) and setWorkflow(false) are used to avoid unnecessary updates or triggering other business rules.
Optional: Force SLA Re-evaluation via Script Include
If the above doesn't work due to SLA conditions not being re-evaluated, you might need to use the SLAReevaluation API:
var reevaluate = new SLAReevaluation();
reevaluate.reEvaluateTaskSLAs(current);
Your Original Script
if (oldSLA.stage != 'cancelled') {
slaUtil.cancelSLA(oldSLA);
oldSLA.update();
}
- Cancels the SLA using slaUtil.cancelSLA().
- Updates the record.
- Does not trigger SLA re-evaluation explicitly.
- ServiceNow's SLA engine does not reattach the same SLA definition unless it detects a change in the task that meets the SLA's start condition after the previous instance was cancelled. Your script cancels the SLA but doesn't trigger that re-evaluation.
My Modified Script
slaUtil.cancelSLA(oldSLA);
oldSLA.update();
}
...
current.autoSysFields(false);
current.setWorkflow(false);
current.update(); // Triggers SLA engine
Stay awesome,
Roshnee Dash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2025 05:12 AM
@Roshnee Dash : I tried implementing the changes suggested but it is still not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2025 01:10 AM
Hi @Akshaykhare
Can you please check once like this
Check SLA Definition Settings:
- Make sure "Reattach on Condition Change" is enabled.
- Ensure "Reset Condition" is configured if needed.
Use a field update (like work_notes, priority, or a custom field) to simulate a change that triggers the SLA engine.
- And if still not working Avoid using setWorkflow(false) if you want the SLA engine to run.
Stay awesome,
Roshnee Dash