- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 04:26 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 05:39 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 04:33 AM
Hi,
you can use 2 more states IN PROGRESS and QUEUED
"stateINWAITING,IN_PROGRESS,QUEUEDstateINWAITING,IN_PROGRESS,QUEUED"
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 04:53 AM
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.');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 05:39 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2021 01:24 PM
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.