Cancel Workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 2 weeks ago
I'm trying to cancel a workflow associated with a story. I'm following this community post which has a correct answer.
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago - last edited 2 weeks ago
All I get when I have "Execute in sandbox" unchecked is this screen like it successfully ran.
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
any flow/workflow is running on that record?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Yes there is a workflow associated to the record to create approvals.
