ServiceNow Kill workflows & executing contexts

 if there are old inactive tickets in the system with active workflows still executing, this can slow down performance of the system. Root cause should be identified, however this script example highlights how these can be shut down in a controlled fashion from a fix script. it should skip business rules and notifications so that end users are not messaged regarding historic inactive tickets

fix script

var si=new tool_improvement();

si.shutDownActiveWFcontexts_CHG(false);//--true flag means it's not query only and will perform the tidy-up

si.shutDownActiveWFcontexts_REQ(false);//--true flag means it's not query only and will perform the tidy-up

script include

var tool_improvement = Class.create(); tool_improvement.prototype = { initialize: function() {}, shutDownActiveWFcontexts_CHG: function(bQueryOnly) { var sLogSource = 'activeworkflowsinactiveCHG'; gs.log("START...", sLogSource); //var sQuery = 'workflow_version.nameLIKEchange^ORworkflow_version.nameLIKEtask^workflow_version.nameNOT LIKErequest'; //sCreatedQuery='started<javascript:gs.beginningOfLast12Months()';
var sQuery='workflow.nameLIKEchange'; var grWF = new GlideRecord('wf_context'); grWF.addQuery('state', 'executing'); grWF.addActiveQuery(); grWF.addNotNullQuery('id'); grWF.addQuery(sQuery); grWF.query(); gs.log('WF row count: ' + grWF.getRowCount(), sLogSource); var sChgArr = []; sChgArr.push('ARR RESULTS:'); var iCounter = 0; while (grWF.next()) { try { //gs.log('RELATED RECORD: ' + grWF.id, sLogSource); var sActiveTableName = this.isInactiveRecord(grWF.id); //gs.log('TABLE NAME: ' + sActiveTableName,sLogSource); if (sActiveTableName == 'change_request' || sActiveTableName =='std_change_proposal') { iCounter++; if (!bQueryOnly) { //--KILL IT! grWF.autoSysFields(false); //--leave last updated intact grWF.setWorkflow(false); //--skip business rules and notifications grWF.setEngines(false); //--skip data policy rules grWF.state = 'cancelled'; grWF.active = false; var cancel_sysid = grWF.update(); this.endWorkflowExecutingActivities(grWF.sys_id, sLogSource); gs.log(cancel_sysid + ': cancelled workflow context [for table record:' + sActiveTableName + ' with sys_id=' + grWF.id, sLogSource); } else { sChgArr.push(sActiveTableName + ";" + grWF.id + "; wf context=" + grWF.sys_id); } } } catch (ex) { gs.log('ERROR [shutDownActiveWFcontexts_CHG]::' + ex.toString(), sLogSource); } } if (iCounter > 0) { gs.log(iCounter + ' workflow contexts to be de-activated', sLogSource); } if (bQueryOnly) { gs.log("END--array:" + sChgArr + "", sLogSource); } gs.log("END SCRIPT", sLogSource); }, shutDownActiveWFcontexts_REQ: function(bQueryOnly) { var sLogSource = 'activeworkflowsinactivereqs'; gs.log("START...", sLogSource); var sQuery = 'workflow_version.nameLIKErequest^ORworkflow_version.nameLIKEOnboard^ORworkflow_version.nameLIKERemove_Admins^ORworkflow_version.nameLIKEService Catalog - New Starter OG - REFORM^ORworkflow_version.nameLIKEMagistrate Rota - Other^ORworkflow_version.nameLIKESN Licensing Workflow^ORworkflow_version.nameLIKEIVR Workflow^ORworkflow_version.nameLIKECatalog^workflow_version.nameNOT LIKEchange'; //sCreatedQuery='started<javascript:gs.beginningOfLast12Months()';

var grWF = new GlideRecord('wf_context'); grWF.addQuery('state', 'executing'); grWF.addActiveQuery(); grWF.addNotNullQuery('id'); grWF.addQuery(sQuery); grWF.query(); gs.log('WF row count: ' + grWF.getRowCount(), sLogSource); var sReqArr = []; sReqArr.push('ARR RESULTS: '); var iCounter = 0; while (grWF.next()) { try { var sActiveTableName = this.isInactiveRecord(grWF.id); //gs.log('TABLE NAME: ' + sActiveTableName,sLogSource); if (sActiveTableName == 'sc_req_item' || sActiveTableName == 'sc_request') { iCounter++; if (!bQueryOnly) { //--KILL IT! grWF.autoSysFields(false); //--leave last updated intact grWF.setWorkflow(false); //--skip business rules and notifications grWF.setEngines(false); //--skip data policy rules grWF.state = 'cancelled'; grWF.active = false; var cancel_sysid = grWF.update(); //--mop up any activities: this.endWorkflowExecutingActivities(grWF.sys_id,sLogSource); gs.log(cancel_sysid + ': cancelled workflow context [for table record:' + sActiveTableName + ' with sys_id=' + grWF.id, sLogSource); } else { sReqArr.push(sActiveTableName + ";" + grWF.id + "; wf context=" + grWF.sys_id); } //break; } } catch (ex) { gs.log('ERROR [shutDownActiveWFcontexts_REQ]::' + ex.toString(), sLogSource); } } if (iCounter > 0) { gs.log(iCounter + ' workflow contexts to be de-activated', sLogSource); } if (bQueryOnly) { gs.log("END--array:" + sReqArr + "", sLogSource); } gs.log("END SCRIPT", sLogSource); }, isInactiveRecord: function(sysid) { //--if the target record is active=false, return the table name //--otherwise, return blank string var sReturn = ''; var grTask = new GlideRecord('task'); grTask.addInactiveQuery(); grTask.addQuery('sys_id', sysid); grTask.query(); if (grTask.next()) { sReturn = grTask.sys_class_name; } return sReturn; }, endWorkflowExecutingActivities: function(context_id, logSource) { var grExecActivities = new GlideRecord('wf_executing'); grExecActivities.addQuery('context', context_id); grExecActivities.addQuery('state', 'waiting'); grExecActivities.query(); while (grExecActivities.next()) { try { grExecActivities.autoSysFields(false); //--leave last updated intact grExecActivities.setWorkflow(false); //--skip business rules and notifications grExecActivities.setEngines(false); //--skip data policy rules grExecActivities.state = 'cancelled'; grExecActivities.update(); gs.log('wf_executing ' + grExecActivities.sys_id + ' shut down', logSource); } catch (ex) { gs.log('ERROR [endWorkflowExecutingActivities]:: ' + ex.toString(), logSource); } } }, type: 'tool_improvement' };
};

Comments

Popular posts from this blog

GlideRecord setValue

URL link in addInfoMessage