Is it possible to update the Stop Date of a task SLA to match the Resolve Date of an incident record whilst running Fix SLAs?

matthew_hughes
Kilo Sage

Hi,

On our instance we're wanting the 'Fix SLAs' functionality to update the Stop Time of the related Task SLAs to match the Resolved Date in the incident as the new Task SLA is created. I was just wondering if this was possible to do because at the moment, the Stop Time of a Task SLA matches the date and time when the incident gets set to 'Closed State'.

At the moment this occurs;find_real_file.png

 

find_real_file.png

find_real_file.png

 

What we would like to happen when the 'Repair SLAs' function is ran:

find_real_file.png

 

find_real_file.png

 

if somebody could please how I would need to implement the repair SLA function, but allow the Stop Date to match the Resolved Date, that would be great. My script is:

var tab = new GlideRecord('task_sla');
//Searches for the affected incident SLAs
tab.addEncodedQuery("task=1c741bd70b2322007518478d83673af3");
//Runs the query
tab.query();
//Helps in prevent running of BR's while updating
tab.setWorkflow(false);
//While it's looping through the records
while(tab.next()){
tab.end_time = tab.task.ref_incident.resolved_at;
tab.update();
}

20 REPLIES 20

Hi Matthew,

To answer your question directly - Yes, it is possible to run the repair function from a script.

However, please proceed with caution and test, test, test in sub prod instances - there's a lot that can go wrong if not done properly, i.e. notifications shooting out, etc.

I would steer you to the manual process but appreciate there could be a large number. You can repair SLAs from the list view, I would recommend using the OOTB methods rather than trying to use a Fix Script.


Also ensure the property for using the Repair SLA workflow is turned on prior to attempting to repair, else it may try to use the original SLA workflow.
https://docs.servicenow.com/bundle/paris-it-service-management/page/product/service-level-management/concept/c_RepairSLAs.html

A script snippet for you to test with and adpat for you use is below

To help others, please mark correct and/or helpful.


Thanks,
Robbie


var incsToRepairGR =  new GlideRecord('incident');
incsToRepairGR.addQuery("<Add your filter/query to only select the incident for repair>"); // update accordingly.
incsToRepairGR.query();
incsToRepairGR.info("Number of incidents to update " + incsToRepairGR.getRowCount());
while(incsToRepairGR.next()){
  var slaRepairGR = new SLARepair();
  var sysId = incsToRepairGR.getValue('sys_id');
  gs.info("Updating incident " + sysId);
  slaRepairGR.repairBySysId(sysId, "incident");
}

Hi Matthew,

Please can I ask you mark my response(s) helpful and correct once you've implemented the changes.

This helps others with similar questions and improves the knowledge base.

Thanks,

Robbie

I appreciate your help. Is it possible to combine the two together so that the script updates the 'Stop time' on the task SLA to reflect the 'Resolved Time'' and fix the SLA? The number of affected task SLA records is 1000.

Hi Matthew,

I've combined the scripts, however please note I have NOT tested. I recommend you perform rigorous ensuring no notifications or any other events are triggered as part of this. ServiceNow themselves do not encourage this used as a script.

Please can you mark correct and my previous responses as helpful. This helps others with similar questions and improves the knowledge base.

Thanks,

Robbie

 

Script:
var total = 0; //Counter used for testing. Confirming total number of records
var count = 0; //Counter used for testing. Confirming how many records to update
//Searches for the affected incident SLAs
var tab = new GlideRecord('task_sla');
tab.addEncodedQuery('end_timeISNOTEMPTY');
tab.addQuery('task.sys_class_name', 'incident');
tab.setLimit(5); //Strongly encourage to use the setLimit() during testing and execution. I would actually recomment controlling the records in the query
//Runs the query
tab.query();
//While it's looping through the records
while(tab.next()){
//Only query/update records where SLA stop time and related Incident Resolved time are different
if(tab.end_time.getDisplayValue() != tab.task.resolved_at.getDisplayValue()){
//Helps in prevent running of BR's while updating
gs.log('found: ' +tab.task.number + 'SLA Def:' + tab.sla.name + ' SLA stop time: ' + tab.end_time.getDisplayValue() + ' inc resolved at: ' + tab.task.resolved_at.getDisplayValue()) ; //used for testing
//tab.end_time = tab.task.resolved_at;
//tab.setWorkflow(false);
//tab.update(); //Uncomment when you have confirmed the correct records are to be updated
var slaRepairGR = new SLARepair();
var sysId = tab.task.sys_id;
gs.info("Updating incident: " + sysId + ' Incident number:' + tab.task.number);
slaRepairGR.repairBySysId(sysId, "incident");
count++;
}
total++
}

 

Hi Matthew,

Circling back on this. How are you going?

I assume you've read and referenced the SLA Repair API documentation:

https://developer.servicenow.com/dev.do#!/reference/api/rome/server_legacy/c_SLARepair

Please can you mark correct and my previous responses as helpful. This helps others with similar questions and improves the knowledge base.

Thanks,

Robbie