logo

NJP

How to reset/abort/cancel a workflow (cleanly)

Import · Sep 18, 2019 · article

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

View original source

https://www.servicenow.com/community/now-platform-articles/how-to-reset-abort-cancel-a-workflow-cleanly/ta-p/2327428