Flow designer: Filter the desired context in sys_flow_context in order to cancel it

Ihor
Giga Expert

Hi, I'm triggering my flow through the script in my server-side UI action "Request banking data" from the "Claim's" form. 

try {
	var inputs = {};
	inputs['current'] = current;
	inputs['table_name'] = 'u_club_claim';
	sn_fd.FlowAPI.startFlow('decision_logic_request', inputs);
} catch (ex) {
	var message = ex.getMessage();
	gs.error(message);
}

Then I will need to cancel this flow, I successfuly can do it with following script that I found

var flowContextGr = new GlideRecord('sys_flow_context');
flowContextGr.addQuery('source_record', 'e9729d21dbb09c105f65ba03e29619fd'); // NEED REPLACE THIS!!!
flowContextGr.addEncodedQuery('state=WAITING^ORstate=IN_PROGRESS^ORstate=QUEUED');
flowContextGr.query();
while(flowContextGr.next()){
	var gpa = new sn_ph.GlideProcessAutomation(flowContextGr.getUniqueValue());
	gpa.cancel('Cancelled from button "Request banking data"');
}

BUT! I can't properly query it, because source_record(document id type) field, is populated not by my record (like in triggering by condition), but with sys_id of my UI action, so I can't find in sys_flow_context my context by sys_id of my record.

This is how reference looks like:
find_real_file.png

Is there a way how I can set this source_record field manually from snippet, or maybe can I query my flow context through some other tables?

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

.startFlow returns you contextid and that contextID can be used to cancel the flow. Update below line in your script to store the contextID in a variable.

var contextId = sn_fd.FlowAPI.startFlow('decision_logic_request', inputs);

 

Use below line to cancel the flow.

sn_fd.FlowAPI.cancel(contextId, 'Flow took too long to execute.'); // Call the cancel() method using the context Id returned from the startFlow() method

 

Please mark this accepted & helpful if it answered your question. Thanks! 

Regards,
Muhammad

View solution in original post

6 REPLIES 6

MrMuhammad
Giga Sage

.startFlow returns you contextid and that contextID can be used to cancel the flow. Update below line in your script to store the contextID in a variable.

var contextId = sn_fd.FlowAPI.startFlow('decision_logic_request', inputs);

 

Use below line to cancel the flow.

sn_fd.FlowAPI.cancel(contextId, 'Flow took too long to execute.'); // Call the cancel() method using the context Id returned from the startFlow() method

 

Please mark this accepted & helpful if it answered your question. Thanks! 

Regards,
Muhammad

Unfortunately, we are using New York and the .cancel method is not available in scope 😕

Using that method would only work in Orlando. 

Ihor
Giga Expert

1 record (Claim) in my case could have multiple flow executions (4-5), if I will use

var contextId = sn_fd.FlowAPI.startFlow('decision_logic_request', inputs);

I would have to store all this contexId's in some comma-separated field, before cancel all of them at once (when I will need it), like workaround it's fine, but still would have ability to query all of them through sys_flow_context by sys_id of my Claim (record, where flows was triggered of)