Cancel workflow

Kumar38
Kilo Sage

we have a table in custom application scope , that has workflows tied to it. When the review goes into Cancelled , the workflow needs to go be cancelled.

Tried the below script , but the workflow isn't getting cancelled.

Log--C is printed once, but the workflow isn't cancelled. There are 2 workflow contexts for the record, where one is finished , the other one is in progress.

 var grContext = new GlideRecord("wf_context");
    grContext.addQuery('id', current.sys_id);
    grContext.query();
    while (grContext.next()) {
        gs.info('Log--C');
        new global.Workflow.cancelContext(grContext.sys_id);
    }

 

 

1 ACCEPTED SOLUTION

Manmohan K
Tera Sage

Hi @Kumar38 

 

You can use below code to cancel all running workflows for a given record

var workflow = new global.Workflow();

workflow.cancel(current);

 

View solution in original post

3 REPLIES 3

Manmohan K
Tera Sage

Hi @Kumar38 

 

You can use below code to cancel all running workflows for a given record

var workflow = new global.Workflow();

workflow.cancel(current);

 

Manmohan K
Tera Sage

@Kumar38 

 

If you want to only cancel a particular workflow associated with the ‘current’ record, then use below code

 

//Query for all executing workflow contexts

var flows = new Workflow().getRunningFlows(current);

while(flows.next()){

//Check for associated workflows by name - Example Routine Change is being cancelled

if(flows.workflow_version.getDisplayValue() == 'Routine Change'){      

//Cancel the workflow context

new Workflow().cancelContext(flows);
}
}

 

 

VerdaKosnett1
Tera Guru

Above recommendations no longer work because ServiceNow added a new BR to prevent the change. So to accomplish the change run the following script:

var gr = new GlideRecord("sc_cat_item");
gr.get('ae4d843d1b41e61009b2ed34b24bcb7c'); //Sys_id of catalog item to be changed to record producer

var workflow = new global.Workflow();

workflow.cancel(gr);
gr.request_method='';
gr.no_cart_v2=0;
gr.no_delivery_time_v2=0;
gr.no_quantity_v2=0;
gr.sys_class_name="sc_cat_item_producer";
gr.table_name='sn_hr_core_case_talent_management'; <---record producer table.
gr.update();