Skip to main content

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

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Service Catalog: variable advanced reference qualifiers

Call a script include to apply a reference qualifier on a catalog item variable: - variable reference qualifier dependent on another variable selection, in this case a variable referencing sys_user (requested_for) On the catalog item form. variable name to apply ref qual filter : retail_equipment variable reference qualifier (on cmdb table ): javascript : new  refqual_functions (). lostStolen_getAssignedCIs (); client-callable script include ( refqual_functions)  function : lostStolen_getAssignedCIs : function (){         //--called from variable set client script, for lost/stolen request (service catalog)     gs . log ( current . variables . requested_for , 'retail_lostStolen_getAssignedCIs' );         return ( 'install_statusNOT IN8,7^owned_by=' + current . variables . requested_for );             //owned_by=1269b79937f1060041c5616043990e41^install_statusNOT IN8,7            },