Cancel running Flow contexts

harun_isakovic
Mega Guru

Hello Community,

I want to cancel running Flow contexts with a certain query. These flow contexts are related to many different RITMs so it's not just contexts for 1 RITM, but for like 1000 RITM.

Encoded query would be "state=WAITING^sys_created_bySTARTSWITHabde^name=FlowName"

Could someone assist with this?

 

1 ACCEPTED SOLUTION

Hi,

sample script below

(function() { 

var now_GR = new GlideRecord('incident'); 
now_GR.addQuery('number', 'INC0000050'); 
now_GR.query(); 
if(now_GR.next()){

// get the flow context for this INC

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

}

}

})();

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use 2 more states IN PROGRESS and QUEUED

"stateINWAITING,IN_PROGRESS,QUEUEDstateINWAITING,IN_PROGRESS,QUEUED"

Regards
Ankur

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

Hi Ankur, the query I provided is the one I want but what would be the script? I'm not so familiar with the Flow API especially to use it on such a high amount of records.

I have this example for a single record from the Flow API documentation, but I would need to make it for the mentioned query.

(function() { 

var now_GR = new GlideRecord('incident'); 
now_GR.addQuery('number', 'INC0000050'); 
now_GR.query(); 
now_GR.next(); 

      try { 
          var inputs = {}; 
          inputs['current'] = gr; // GlideRecord of table: 
          inputs['table_name'] = 'incident'; 

          // Starts the flow asynchronously.
          var contextId = sn_fd.FlowAPI.startFlow('global.myFlow', inputs); 

      } catch (ex) { 
          var message = ex.getMessage(); 
          gs.error(message); 
      } 
})();

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

Hi,

sample script below

(function() { 

var now_GR = new GlideRecord('incident'); 
now_GR.addQuery('number', 'INC0000050'); 
now_GR.query(); 
if(now_GR.next()){

// get the flow context for this INC

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

}

}

})();

Regards
Ankur

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

I would suggest using the .cancel method for the flow with this. The sn_ph api is undocumented and I found the .cancel method to be a little more efficient.