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-upsi.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.nameLIKEchangeORworkflow_version.nameLIKEtaskworkflow_version.nameNOT LIKErequest'; //sCreatedQuery='started 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.nameLIKErequestORworkflow_version.nameLIKEOnboardORworkflow_version.nameLIKERemove_AdminsORworkflow_version.nameLIKEService Catalog - New Starter OG - REFORMORworkflow_version.nameLIKEMagistrate Rota - OtherORworkflow_version.nameLIKESN Licensing WorkflowORworkflow_version.nameLIKEIVR WorkflowORworkflow_version.nameLIKECatalogworkflow_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' };
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 )){
Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }
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,7owned_by=' + current . variables . requested_for ); //owned_by=1269b79937f1060041c5616043990e41install_statusNOT IN8,7 },
http://www.cloudminus89.com/2022/07/servicenow-kill-workflows-executing.html