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.

petercawdron
Kilo Guru

There are times where you simply want to abort a workflow during the approval process. You don't want to mess around with stuff and just want a clean start. Here's how you can do that from Script Includes (triggered by a UI action), simply pass the current record through to this script and you'll find yourself back where you started.

//First, reverse the existing approvals
var approvals = new GlideRecord('sysapproval_approver');
approvals.addEncodedQuery('^sysapproval=' + current.sys_id + '^ORdocument_id=' + current.sys_id);
approvals.query();
while(approvals.next()){
	approvals.state = 'not requested';  
	approvals.comments = 'This approval was manually reset';
	approvals.update();
}

//Which workflow are we working with?
var thisWorkFlow = '';
var whichWorkFlow = new GlideRecord('wf_context');
whichWorkFlow.addEncodedQuery('id='+current.sys_id);
whichWorkFlow.setLimit(1);
whichWorkFlow.query();

if(whichWorkFlow.next()){thisWorkFlow = whichWorkFlow.workflow_version.toString();}

//Now delete the current workflow
new Workflow().deleteWorkflow(current); 

//Reset this record back to not requested and a draft state
current.approval = 'not requested';
current.state = '1';//Draft
current.update();

//Finally, restart the workflow
if(thisWorkFlow!=''){
	var wf = new Workflow();
	var context = wf.startFlow(thisWorkFlow, current, current.update());
}

Have fun

Version history
Last update:
‎09-17-2019 11:12 PM
Updated by: