How to kill flow using script and trigger it using script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2025 04:40 AM
Hi,
im calling a client script that calls a script include and pass as a parameter a sys_id of a change_request.
How can I kill the current flow (not workflow) on the change_request and then trigger a new flow using his sys_id using using on the change_request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2025 04:49 AM
Refer : https://www.servicenow.com/community/developer-forum/canceling-flow-using-script/m-p/2760098
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2025 05:04 AM - edited 07-28-2025 05:04 AM
You can use the FlowAPI , working both scoped and global:
https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...
Just use the .cancel(String contextId, String reason) method. You just need the sys_id of the running flow and pass-it with a reason.
You can also find examples there:)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2025 05:12 AM
something like this
// get the flow context for this INC
var sysId = ''; // your CHG sysId
var flow = new GlideRecord("sys_flow_context");
flow.addQuery('name', 'myFlow'); // flow name
flow.addQuery('source_record', sysId);
flow.addEncodedQuery('stateINWAITING,IN_PROGRESS,QUEUED');
flow.query();
wifhile(flow.next()) {
var gpa = new sn_ph.GlideProcessAutomation(flow.getValue('sys_id'));
if (!gpa.isTerminated()) {
//cancel the flow and provide the reason
gpa.cancel('Cancelling this flow');
}
}
try {
var chg = new GlideRecord('change_request');
if (chg.get(sysId)) {
var inputs = {};
inputs['current'] = chg; // GlideRecord of table:
inputs['table_name'] = 'change_request';
// Execute Synchronously: Run in foreground.
// var timeout = ; //timeout in ms
//sn_fd.FlowAPI.executeFlow('global.test_flow', inputs, timeout)
sn_fd.FlowAPI.executeFlow('<flowName>', inputs); // give here complete flow name include scope name
}
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
Sharing links for help
Scripting with Flows, Subflows, and Actions
How do we cancel an executing Flow Context (any table) when a particular condition is met ?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2025 08:17 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader