Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Cancel Workflow

brianlan25
Kilo Patron

I'm trying to cancel a workflow associated with a story. I'm following this community post which has a correct answer.

https://www.servicenow.com/community/itsm-forum/how-can-i-cancel-a-running-workflow-on-a-single-ritm...

However I'm getting an error when using script background and enabling execute in sandbox. The error message is KittyScript validation failed for null.null.script with error: Syntax error: Expected end of expression but found 'gr' at position 4.

Any idea on why this code is not working?

var gr = new GlideRecord("rm_story");
gr.addQuery("number", "STRY517"); //full story number is in the actual code I'm trying to use.
gr.query();
if (gr.next()) {
	// cancel old running workflow
	var flows = new Workflow().getRunningFlows(gr);
	while(flows.next()) {
		new Workflow().cancelContext(flows);
	}  
}

@Ankur Bawiskar 

9 REPLIES 9

Ankur Bawiskar
Tera Patron

@brianlan25 

Did you try unchecking that "Execute in sandbox" checkbox?

also try this updated script

var gr = new GlideRecord("rm_story");
gr.addQuery("number", "STRY517"); //full story number is in the actual code I'm trying to use.
gr.query();
if (gr.next()) {
	// cancel old running workflow
	var flows = new Workflow().getRunningFlows(gr);
	while(flows.next()) {
		new Workflow().cancelContext(flows);
	}  
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

All I get when I have "Execute in sandbox" unchecked is this screen like it successfully ran.

brianlan25_0-1787669141797.png

However the workflow is not canceled. You can ignore the flow.query() I had in my original post that was just something I was trying. I also tried putting a gs.log in the while loop with just gs.log('in while loop') but it never logs it. It is like it is not getting past the var flows = new Workflow().getRunningFlows(gr).

 

Edit:

With the following code doing some logging I only get the first to logs.

var gr = new GlideRecord("rm_story");
gr.addQuery("number", "STRY6517"); //full story number is in the actual code I'm trying to use.
gr.query();
if (gr.next()) {
	// cancel old running workflow
       gs.log("in if");
	var flows = new Workflow().getRunningFlows(gr);
       gs.log("after flows");
	while(flows.next()) {
               gs.log("in while");
		new Workflow().cancelContext(flows);
	}  
}

brianlan25_1-1787669624670.png

 

 

@brianlan25 

any flow/workflow is running on that record?

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

Yes there is a workflow associated to the record to create approvals.