- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 06:39 AM
Hi All,
I need help to cancel the workflow on the workflow context(wf_context) table when variable set (variable) is set to true on the catalog item.
var gr = new GlideRecord ('sc_req_item');
gr.addQuery('sys_id', current.sysapproval.sys_id);
gr.query();
if (gr.next()){
if (gr.variables.ritm.toString() == 'true'){
var flow = new workflow().getRunningFlows(gr);
while (flows.next()){
new Workflow().cancelContext(flows);
}
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 07:44 AM
Hi,
Have you tried to use any log statements to see what is going on? Else you're just coding in the dark and saying your script doesn't work, but you won't know why.
I can only comment on the script itself, but the values and field names is up to you to verify as appropriate.
You can try a script like this:
if (current.variables.ritm.toString() == 'true'){
var wgr = new GlideRecord("wf_context");
wgr.addQuery('id', current.sys_id);
wgr.query();
while(wgr.next()){
new Workflow().cancelContext(wgr);
}
}
You'd use this in a Run Script activity within your workflow. So simply draw a line to this as part of your flow. This will check that variable value and if true for that specific variable, all workflow contexts will be cancelled.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 07:22 AM
The workflow is on sysapproval_approver table and trigger via conditions, when RITM is pending for approval
The above script is in the workflow(run script), I would like to add it in the Advance script of "If" object.
'Yes' would execute the workflow flow
'No' would cancel the workflow
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 07:40 AM
Hi,
update as this
if (current.variables.ritm.toString() == 'true'){
var flow = new workflow().getRunningFlows(current);
while (flows.next()){
new Workflow().cancelContext(flows);
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 07:54 AM
Thanks for the quick response!
This script should be in the advance script of "if" object workflow?
Yes should pass the workflow, No would cancel the workflow.
I tried as the advance script of "if" condition but unfortunately it didn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 08:21 AM
Hi,
your workflow is on catalog item i.e. RITM then it should work fine
did you check variable name is correct?
what's the if script you are using?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 09:38 AM