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.

Canceling flow using script

Vivek Prakash
Tera Contributor

Hi All,

I am required to cancel some redundant flows in waiting stage in flow designer using a script but when i run the script in background I can see some records being deleted namely -

sys_json_chunk and

sys_flow_rw_action

Just wanted to confirm whether it will have an impact or not.

Code -

var flow = new GlideRecord("sys_flow_context");
flow.addQuery('name', 'yourflow'); // your flow name
flow.addQuery('source_record', 'sysId of record');
flow.addEncodedQuery('stateINWAITING');
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 for redundant request');
    }

}

 Thanks,

Vivek

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Vivek Prakash 

script looks fine to me.

Did you check the state got updated?

can you try this

var now_GR = new GlideRecord("sys_flow_context"); 
now_GR.addQuery("name", "NAME OF FLOW TO CANCEL HERE"); 
now_GR.addQuery('source_record', 'sysId of record');
now_GR.addEncodedQuery('stateINWAITING');
now_GR.query(); 
while (now_GR.next()) { 
	sn_fd.FlowAPI.cancel(now_GR.getUniqueValue(), 'Cancelling this flow for redundant request'); 
} 

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

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Vivek Prakash 

script looks fine to me.

Did you check the state got updated?

can you try this

var now_GR = new GlideRecord("sys_flow_context"); 
now_GR.addQuery("name", "NAME OF FLOW TO CANCEL HERE"); 
now_GR.addQuery('source_record', 'sysId of record');
now_GR.addEncodedQuery('stateINWAITING');
now_GR.query(); 
while (now_GR.next()) { 
	sn_fd.FlowAPI.cancel(now_GR.getUniqueValue(), 'Cancelling this flow for redundant request'); 
} 

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