How to cancel a Flow in "Waiting" State?

jordanbarry
Tera Contributor

I have a custom record with a state field. When the field switches to "on hold", it triggers a flow with a timer. If the state switches to something other than "on hold", I want to cancel the flow before the timer ends. 

 

I created a business rule to try and cancel the flow after the state changes.

 

(function executeRule(current, previous /*null when async*/) {
				
	var flowName = 'Customer Pause'; // Flow name
	var tableName = current.getTableName();
	gs.info("Business Rule Started" + current.sys_id + ", " + flowName);

	var grFlowContext = new GlideRecord('sys_flow_context'); // Searches for a flow with the listed name for this current record
	grFlowContext.addQuery('source_record', current.sys_id); 
	grFlowContext.addQuery('source_table', tableName);
	grFlowContext.addQuery('name', flowName); 
	grFlowContext.addQuery('state', 'WAITING'); 
	grFlowContext.query();

	gs.info("Customer Pause Count" + grFlowContext.getRowCount() + " contexts for flow '" + flowName + "'.");
	

	var cancelledCount = 0;
	
	while (grFlowContext.next()) {
		
		// Cancel the flow instance
		gs.info("TEST: Execution Loop");
		sn_fd.FlowAPI().cancel(grFlowContext.getUniqueValue(), 'Flow is no longer needed.');
		cancelledCount++;
		
	}

	if (cancelledCount > 0) {
		gs.info('Canceled ' + cancelledCount + ' previous instances of Flow "' + flowName + '" for record ' + current.number);
	}

})(current, previous);

 

After reviewing the system logs, my business rule is executing correctly and my record query on the sys_flow_context table is working as it returns 17 (and counting for each test). The only issue is my while loop seems to stop. That loop statement is only produced once in the final execution.

 

When I review all the flow records, they all remain in the "WAITING" state. They are not canceling as far as I am aware.

 

I am able to manually cancel the flow in the sys_flow_context record.

 

Am I missing something in my business rule? I am unsure how to use a script to cancel these flows when they are no longer needed.

 

0 REPLIES 0