Cancel RITM and Flow context

Al-JhonV
Mega Sage

Hello All,


I created a after BR to cancel flow_context of the current ritm, the ritm is cancelling but upon checking the flow context record, it is completed instead of cancelled.

Can anyone check my BR?:

var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('flow_context', current.sys_id);
    ritm.query();
 
if(ritm.next()){
 
    var workflow = new global.Workflow();
    workflow.cancel(ritm);
}
2 ACCEPTED SOLUTIONS

@Al-JhonV Please create a BR on sc_req_item table and add below code to it

 

var flowContextGr = new GlideRecord("sys_flow_context");
flowContextGr.addQuery("source_record="+current.sys_id.toString());
flowContextGr.addQuery("name=<YOUR FLOW NAME HERE>");
flowContextGr.query();

if(flowContextGr.next()){
    sn_fd.FlowAPI.cancel(flowContextGr.sys_id.toString(), "manually by " + gs.getSession().getUserName());
}
 
Please mark all the answers on this question as correct answers if this solves your issue.

View solution in original post

Ankur Bawiskar
Tera Patron

@Al-JhonV 

try this once

var flow = new GlideRecord("sys_flow_context");
flow.addQuery('source_record', current.getUniqueValue());
flow.addEncodedQuery('stateINWAITING,IN_PROGRESS,QUEUED');
flow.query();
if(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');
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron

@Al-JhonV 

Please check the flow API documentation.

Ankur Bawiskar
Tera Patron

@Al-JhonV 

try this once

var flow = new GlideRecord("sys_flow_context");
flow.addQuery('source_record', current.getUniqueValue());
flow.addEncodedQuery('stateINWAITING,IN_PROGRESS,QUEUED');
flow.query();
if(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');
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Al-JhonV
Mega Sage

Hello @Ankur Bawiskar and @jaheerhattiwale , thanks for sharing the ideas.
I found out that the BR that i created long time ago was corrupted after the patch upgrade to UTAH.
I just created a new BR with the same script and just inactive the old one.

(function executeRule(current, previous /*null when async*/) {
//If request has a flow desinger execution associated with it, cancel it.

if ((current.stage == 'Request Cancelled') && current.flow_context && !current.flow_context.nil()){
new sn_fd.FlowAPI.cancel(current.flow_context, "by user " + gs.getSession().getUserName());
}
})(current, previous);

@Al-JhonV Thats good. But this is exactly the same solution what I proposed. So can you please mark my answer also as correct answer so that it helps the others.

 

Thanks and regards,

Jaheer