How to cancel current workflow and start a new workflow

Tyler36
Tera Contributor

Hello i am creating a business rule that when triggered it stops the current workflow and will start a different workflow. Any ideas on how to achieve this? Thanks.

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi

To start a new workflow and attach to the ticket

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr, 'insert'); //gr is the record

 

To cancel the workflow

var w = new Workflow();
var gr = new GlideRecord(‘wf_context’);

if (gr.get(current.context)) // current is the record here
w.cancelContext(gr);

-Anurag

-Anurag

View solution in original post

5 REPLIES 5

ersureshbe
Giga Sage
Giga Sage

Hi,

Can you refer this link to understand.

https://community.servicenow.com/community?id=community_question&sys_id=7f864b25db1cdbc01dcaf3231f9619b3

Please mark as correct answer if it helped.

Regards,

Suresh.

Regards,
Suresh.

Anurag Tripathi
Mega Patron
Mega Patron

Hi

To start a new workflow and attach to the ticket

var wflw = new Workflow();
wflw.startFlow(wflw.getWorkflowFromName('give the workflow name'), gr, 'insert'); //gr is the record

 

To cancel the workflow

var w = new Workflow();
var gr = new GlideRecord(‘wf_context’);

if (gr.get(current.context)) // current is the record here
w.cancelContext(gr);

-Anurag

-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

like this

// 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

_vicky01
Mega Contributor

you can try this

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

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