How to restart a workflow on field change?

Snow Tomcal
Tera Expert

Hi,

I'm trying to restart the change request's workflow on a field change with advanced business rule.

The workflow first activity changes to grey (restarted) and start running again but it is not asking for approvals again (the rest of the workflow stays the same, I checked the workflow history so I know it is restarting),

the script:

(function executeRule(current, previous /*null when async*/){

var chng = new GlideRecord('chnage_request');

chng.addQuery('number', current.number);

chng.query();

 

if(chng.next()){

var workflow = new global.Workflow();

workflow.cancel(chng);

workflow.restartWorkflow(chng);

}

})(current, previous);

 

Anyone have any ideas? Thanks! 🙂

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is it happening always?

Did you try to cancel earlier context and start new one?

// cancel old
var flows = new Workflow().getRunningFlows(current);
while(flows.next()) {
	new Workflow().cancelContext(flows);
}

// attach the new one
var wf1 = new Workflow();
wf1.startFlow(wf1.getWorkflowFromName('give the workflow name'), current, 'update');

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I'm trying to restart only the change request workflow, not all the running workflows.

Hi,

the script I shared will cancel existing running workflow on that record and start new one

Did you try that?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Yes, It stops the flow but does not restarts it.