Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Write a fix script to stop/cancel a flow context

keerthana10
Tera Contributor

FIX SCRIPT

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

example below

Give your table name, record sysID etc and flow name

(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'); // your 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

@keerthana 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

Tushar Walveka2
Tera Guru
// Get the flow context for your record
var flowContextGR = new GlideRecord("sys_flow_context");
flowContextGR .addQuery('source_record', 'sys_id'); //sys_id of the record for which the flow needs be cancelled
flowContextGR .query();
if(flowContextGR .next()){
      //cancel the flow
       sn_fd.FlowAPI.cancel(flowContextGR .sys_id, gs.getMessage('manually by {0}', gs.getSession().getUserName()));
}