How do you cancel workflow and restart a new version on a request

curtishampton
Mega Contributor

I need to write a fix script or a simple ui action that will cancel the current workflow on an RITM record and start a new version of the same workflow.

In short, we made changes to fix the workflow for a catalog item.   Now that we have moved it to prod, we need to reset all the RITM records using the old context of the workflow so that it uses the new version of the workflow.

12 REPLIES 12

curtishampton
Mega Contributor

I've reviewed articles and tried the following from a UI Action(Form Button).   The sys id referenced in the startFlow function is coming from the published context of the workflow I want to start in the wf_workflow table:



//cancel workflow.
var oldWorkflow = new Workflow();
oldWorkflow.cancel(current);



//start new workflow
var newWorkflow = new Workflow();
var operation = 'insert';
var context = newWorkflow.startFlow('1dab578213cd32409a3bbda12244b0ea', current, current.operation(), getVars());



function getVars() {
var vars = {};
for (var key in current.variables) {
  vars[key] = current.variables[key];
}
return vars;
}



action.setRedirectURL(current);



Results:



1.)   The old workflow is canceling properly.


2.)   The new workflow is not being started.


Did you get this working?


Shiraz2
Mega Guru

Curtis, I spoke to my Lead Developer about this, and he confirms my suspicion that once an Item is requested, a Workflow gets associated with it. Now, even if you were to edit the same workflow, you can stop the workflow, but it will still start the original one. Same goes for a new workflow. The only way to get the new WorkFlow to work is to Re-Request the same item with that new workflow.



you can still continue to ask this question here. Maybe someone else has a work around.


benoit_auneau1
Kilo Contributor

Hi Curtis,



I made a quick test from your code, and with this new one I got something working :



var gr = new GlideRecord('sc_req_item');


gr.get('bf1ced7737783e0087ced0d543990e9f'); // my test item I want to restart sys_id



var oldWorkflow = new Workflow();


oldWorkflow.cancel(gr);



var newWorkflow = new Workflow();


newWorkflow.startFlow(new Workflow().getWorkflowFromName('MY WF NAME'), gr, '');



Let me know if this helps !


Obah
Tera Contributor

Hi Curtis

I ran into the same need and this worked perfectly,kicking in the new version of workflow:

 

var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", "6eea0285db419c10053830cf9d9634d4");
gr.query();
if (gr.next()) {

new WorkflowApprovalUtils().cancel(gr);
new Workflow().startFlow(new Workflow().getWorkflowFromName('myWorkflowName'), gr, '');
}