How to kill flow using script and trigger it using script

Alon Grod
Tera Expert

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

7 REPLIES 7

Martin Virag
Tera Guru

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:)

Ankur Bawiskar
Tera Patron
Tera Patron

@Alon Grod 

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.

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

@Alon Grod 

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.

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