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

Ankur Bawiskar
Tera Patron
Tera Patron

@Al-jhon 

Please check the flow API documentation.

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

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

Al-jhon
Kilo 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-jhon 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

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