The CreatorCon Call for Content is officially open! Get started here.

How to cancel particular existing running workflow

Rameshnathan
Tera Expert

Hi

I have one custom table and i am inserting record on the table whenever changing state is pending the Incident table

1. Once Record is created on my custom table one workflow will run on the record

2. if they changing the state form pending to active then again changing active to Pending state in incident table means another one record will insert in my custom table

in this case i want to cancel my previous record workflow

How to achieve this..?

Thanks

1 ACCEPTED SOLUTION

Hi Ashutosh,



Can i do like this,



I have running one Before Insert BR on my custom table and running this script....



var rec = new GlideRecord('on_hold_state');


rec.addQuery('number', current.number);


rec.query();


if (rec.next()) {


var workflow = new global.Workflow();


workflow.cancel(rec);


gs.info("Existing Recors is " + rec.sys_id);



}



actually this is working but i just want to know whether this is correct method or not


View solution in original post

8 REPLIES 8

Thank you. i'm going to use this only


If you dont mind can you mark answer as correct and close this thread.


Thanks, this post helped me as well.

/Petr

hickoryal
Giga Contributor

Here is a simple piece of code to cancel a workflow.

var gr = new GlideRecord('wf_context');
gr.get(<workflowcontext_you_want_to_cancel>);
new Workflow().cancelContext(gr);

 


var wgr = new GlideRecord("wf_context");
wgr.addQuery("workflow_version",'a899f32edb5ff300d0f770c08c96197c');  // Add this if you want to cancel all active workflow contexts related to a given workflow.
wgr.addQuery("started_by",'e48daa51dbe5a700d0f770c08c961931');   // add the sysid of a user who started the workflow
wgr.query();

while(wgr.next()){
gs.print(wgr.id.number);
var gr = new GlideRecord('wf_context');
gr.get(wgr.sys_id);
new Workflow().cancelContext(gr);
}