Cancel and Restart Workflow from OUTSIDE the table against which workflow is executing
At my current client in the aerospace industry, some custom work was built to integrate with an external purchasing system which requires initiating the order process via catalog item then waiting for some time and rechecking to see if the purchasing system has processed the request, pick up any errors generated and/or proceed with request closure.
In the case of certain errors, it was found there was a need to cancel the execution of workflow sitting in a 'waiting' state and restart.
To this end, the following script was written. The challenge here was that it had to run outside of a given record so we couldn't use the standard "current." to instance a glide record.
var current = 'POL0010239';
var gr = new GlideRecord ('proc_po_item');
gr.addQuery('number', current);
gr.query();
if(gr.next())
{
var oldWorkflow = new Workflow();
oldWorkflow.cancel(gr);
var newWorkflow = new Workflow();
newWorkflow.startFlow(new Workflow().getWorkflowFromName('Procurement Integration Workflow'), gr, '');
}
https://www.servicenow.com/community/developer-articles/cancel-and-restart-workflow-from-outside-the-table-against-which/ta-p/2324204