Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

sla bulk repair

El Cuchi
Tera Guru

hi all,

i trust this email finds you well.

i created an sla for group approval. after a few months i realised the duration was incorrect. so i changed the duration and new approvals have the correct times. However, i would like to correct old change approvals to keep reports and metrics correct. I found 39K sla's linked to group approvals and to repair them i created the below fix script.

var gr = new GlideRecord('sysapproval_group');
gr.addEncodedQuery("parent.sys_class_name=change_request^NQassignment_group=NULL^NQassignment_groupNOT INfe1fd6f1db8f3740f0195d87f4961972,d5950787db8f7300f0195d87f4961999");
gr.query();

// Create an instance of the SLA Repair API (server-side)
var repair = new SLARepair();

while (gr.next()) {

	var sysId = gr.sys_id.toString();
    
    // Repair SLAs
    repair.repairBySysId(groupApprovalSysId, 'sysapproval_group');

    // Manually reset the task_sla.task field
    var slaGR = new GlideRecord('task_sla');
    slaGR.addQuery('task', gr.parent); // Check if it was wrongly assigned to Change Request
	slaGR.addQuery('sla.name', 'Assignment Group Approval');
    slaGR.query();
    while (slaGR.next()) {
        slaGR.task = groupApprovalSysId; // Reset task back to the correct sysapproval_group
        slaGR.update();
    }

}

 

when i run this script for one specific group approval i works fine, but when running it for the 39K records i end-up having only sla for 40 group approvals and the other 38K are not longer there.

 

would you know why?

 

regards.

4 REPLIES 4

sandeepchau
Tera Contributor

Try to below in Non-Prod and on a test subnet. :-

Also try to use error handling mechanisms and logging for effective troubleshooting.

 

// Run this script in a background script or Fix Script
(function() {
// Query sysapproval_group records
var gr = new GlideRecord('sysapproval_group');
gr.addEncodedQuery("parent.sys_class_name=change_request^NQassignment_group=NULL^NQassignment_groupNOT INfe1fd6f1db8f3740f0195d87f4961972,d5950787db8f7300f0195d87f4961999");
gr.query();

// Initialize SLARepair API
var repair = new SLARepair();

// Counter for logging
var processedCount = 0;
var batchSize = 100; // Process in batches to avoid timeouts
var logMessages = [];

// Backup task_sla records (optional, for safety)
var backupSLA = new GlideRecord('task_sla');
backupSLA.addQuery('sla.name', 'Assignment Group Approval');
backupSLA.query();
var backupData = [];
while (backupSLA.next()) {
backupData.push({
sys_id: backupSLA.sys_id.toString(),
task: backupSLA.task.toString(),
sla: backupSLA.sla.toString()
});
}
gs.info('Backed up ' + backupData.length + ' task_sla records');

// Process sysapproval_group records
while (gr.next()) {
try {
var sysId = gr.sys_id.toString(); // Correct variable

// Step 1: Repair SLAs for this sysapproval_group
repair.repairBySysId(sysId, 'sysapproval_group');
logMessages.push('Repaired SLAs for sysapproval_group: ' + sysId);

// Step 2: Update task_sla.task field if necessary
var slaGR = new GlideRecord('task_sla');
slaGR.addQuery('task', gr.parent); // SLAs linked to Change Request
slaGR.addQuery('sla.name', 'Assignment Group Approval');
slaGR.query();
while (slaGR.next()) {
slaGR.task = sysId; // Reassign to sysapproval_group
slaGR.update();
logMessages.push('Updated task_sla ' + slaGR.sys_id + ' to task: ' + sysId);
}

processedCount++;
if (processedCount % batchSize === 0) {
gs.info('Processed ' + processedCount + ' records');
// Optional: Flush logs to avoid memory issues
gs.info(logMessages.join('\n'));
logMessages = [];
}
} catch (e) {
logMessages.push('Error processing sysapproval_group ' + gr.sys_id + ': ' + e);
}
}

// Log final results
gs.info('Completed processing ' + processedCount + ' sysapproval_group records');
gs.info(logMessages.join('\n'));

// Verify task_sla records
var finalSLA = new GlideRecord('task_sla');
finalSLA.addQuery('sla.name', 'Assignment Group Approval');
finalSLA.query();
var finalCount = finalSLA.getRowCount();
gs.info('Final task_sla count for Assignment Group Approval: ' + finalCount);

// Save backup data to a log or table (optional)
gs.info('Backup data available for ' + backupData.length + ' records');
})();

 

 

 

shantanu_patel8
Mega Guru

Hi @El Cuchi ,

 

I feel you have not shared the whole script as "groupApprovalSysId" is not defined. Looks like a syntax error to me. Try changing "groupApprovalSysId" to "sysId" defined in the line just above it.

 

You can also refer : https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

 

Please mark the answer helpful and correct if it resolves the issue. Happy Scripting 🙂

-Shantanu

sorry i modified code while testing different options.

var gr = new GlideRecord('sysapproval_group');
gr.addEncodedQuery("parent.sys_class_name=change_request^assignment_group!=fe1fd6f1db8f3740f0195d87f4961972^ORassignment_group=NULL^assignment_group!=d5950787db8f7300f0195d87f4961999^ORassignment_group=NULL"); 
gr.query();


// Create an instance of the SLA Repair API (server-side)
var repair = new SLARepair();

while (gr.next()) {

	// --------- new code -------
	    var sysId = gr.sys_id.toString();
    
    // Repair SLAs
    repair.repairBySysId(gr.sysId, 'sysapproval_group');

    // Manually reset the task_sla.task field
    var slaGR = new GlideRecord('task_sla');
    slaGR.addQuery('task', gr.parent); // Check if it was wrongly assigned to Change Request
    slaGR.query();
    while (slaGR.next()) {
        slaGR.task = gr.sys_id; // Reset task back to the correct sysapproval_group
        slaGR.update();
    }

}

1. CTry replacing "repair.repairBySysId(gr.sysId, 'sysapproval_group')" to "repair.repairBySysId(sysId,'sysapproval_group')". 

 

gr.sysId is again wrong as it should be gr.sys_id. Also the format requires the sys_id to be string.

 

2. Put a gs.info(gr.getRowCount()); before while(gr.next()) line so that you know how many records are getting impacted.

 

Please mark the answer helpful and correct if it helps the issue. Happy scripting 🙂

-Shantanu