Cancel RITM and Flow context

Al-jhon
Kilo 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-jhon 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.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Al-jhon 

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.

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

View solution in original post

8 REPLIES 8

jaheerhattiwale
Mega Sage
Mega Sage

@Al-jhon use sn_fd.FlowAPI to cancel the flow context.

 

Please check below link, you will find code for cancelling flow context.

 

https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server/sn_fd-namespace/ScriptableFlowA...

 

Please mark as correct answer if this solves your issue 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hello @jaheerhattiwale , i updated my script into:

var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('flow_context', current.sys_id);
    ritm.query();
 
if(ritm.next()){
 
sn_fd.FlowAPI.cancel(ritm.getUniqueValue(), "manually by " + gs.getSession().getUserName());
}
 
})(current, previous);


But the flow context state is still completed though the RITM state has been cancelled.
So only the state on the flow context record is complete and not cancelled. Any idea?

Aljhon_0-1685521728504.png

 

@Al-jhon to the cancel function you must pass the sys I'd of flow context in first parameter.

 

But currently you are passing ritm sys I'd as first parameter.

 

So please pass flow context sys I'd as first parameter to cancel function.

 

To get flow context sys I'd you need to query sys_flow_context table.

 

Please mark all the answers on this question as correct answers if this solves your issue 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

@Al-jhon 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.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023