Business Rule to Cancel a flow, but not all of them

Community Alums
Not applicable

I have a business rule on a table that runs what we can Service Requests. The rule is working, but it's canceling all of the running flows (Not the desired results).  I want it only to cancel the context of the changed record.

The business rule is setup as follows:
Active: True
Advanced: True
Table: Service Request
When to Run Section: async
Update: True
Filter Condition: State changes from "Waiting-Client"
Nothing on the Actions tab:
Advanced Tab has the following code:

(function executeRule(current, previous /*null when async*/) {
        var gr = new GlideRecord("sys_flow_context");
	gr.addQuery("name","SR eLearning/Threat emulation Nudge");
	gr.query();
	while (gr.next()){
		sn_fd.FlowAPI.cancel(gr.getUniqueValue(), 'Canceling Nudge due to state change');
	}
})(current, previous);

How can I stop just the flow that is connected to the specific Service Request and not all of them?

1 ACCEPTED SOLUTION

Ethan Davies
Mega Sage
Mega Sage

Hey @Stephen C ,

You can cancel an individual flow context by modifying your code as below. The example below is me trying to cancel a Flow for a Request I have made.

var grRequest = new GlideRecord('sc_req_item');
// Enter the sys_id of your current record here, current.getValue('sys_id');
if (grRequest.get('6b6bc3a5970111102d2a38271153afe8')) {
    var flowContext = new GlideRecord("sys_flow_context");
    flowContext.addQuery('name', 'Service Catalog Item Request'); // flow name
    flowContext.addQuery('source_record', grRequest.getValue('sys_id'));
    flowContext.addEncodedQuery('stateINWAITING,IN_PROGRESS,QUEUED');
    flowContext.query();
    flowContext.setLimit(1);
    while (flowContext.next()) {
        var gpa = new sn_ph.GlideProcessAutomation(flowContext.getValue('sys_id'));
        if (!gpa.isTerminated()) {
            gpa.cancel('Flow has been cancelled');
        }
    }
}

Credit due to this post here.

Thanks

Please mark this answer as correct or helpful if it solved your issue, thanks!

View solution in original post

2 REPLIES 2

Ethan Davies
Mega Sage
Mega Sage

Hey @Stephen C ,

You can cancel an individual flow context by modifying your code as below. The example below is me trying to cancel a Flow for a Request I have made.

var grRequest = new GlideRecord('sc_req_item');
// Enter the sys_id of your current record here, current.getValue('sys_id');
if (grRequest.get('6b6bc3a5970111102d2a38271153afe8')) {
    var flowContext = new GlideRecord("sys_flow_context");
    flowContext.addQuery('name', 'Service Catalog Item Request'); // flow name
    flowContext.addQuery('source_record', grRequest.getValue('sys_id'));
    flowContext.addEncodedQuery('stateINWAITING,IN_PROGRESS,QUEUED');
    flowContext.query();
    flowContext.setLimit(1);
    while (flowContext.next()) {
        var gpa = new sn_ph.GlideProcessAutomation(flowContext.getValue('sys_id'));
        if (!gpa.isTerminated()) {
            gpa.cancel('Flow has been cancelled');
        }
    }
}

Credit due to this post here.

Thanks

Please mark this answer as correct or helpful if it solved your issue, thanks!

Community Alums
Not applicable

@Ethan Davies Thank you very much.  I just did a little tweaking to the code and I believe it's working.  Thank you so very much for the quick response and the answer.  Much appreciated.