How to stop a workflow

Dizy M
Tera Expert

Hi everyone, need help on this. 

 

A little background, we have an existing business rule wherein if the request is cancelled then the task should be cancelled too but it doesnt stop the workflow.  So I tried to create a new business rule wherein if the task is cancelled then it will stop sending the warning email but this one still sending a warning email eventhough I cancelled the request before the SLA reached 47%. The workflow is still on "Executing" state.

 

BR: 

DizyM_0-1688969175392.png

DizyM_2-1688969237021.png

 

Workflow: 

DizyM_3-1688969424406.png

 

 

Thank you in advance!

9 REPLIES 9

Avinash Uskamal
Tera Contributor

When: After

Insert: True

Update: True

Condition: current.active.changesTo(false)

new Workflow().cancel(current); //add to script

hi @Avinash Uskamal thank you. I tried this and the workflow still executing i dont know why

 

DizyM_0-1688976063861.png

 

Anup Desai1
Mega Sage

Hi @Dizy M ,

 

There are methods in the Workflow API to do that.

https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_Workflow_Workflow

 

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

var workflow = new global.Workflow();

workflow.cancel(current);

 

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);
}
}

Please mark the answer correct/helpful based on Impact.
Regards, Anup

Hi @Anup Desai1 thank you for the reply . I tried the first one but it seems the workflow is still executing and running..  But when I tried the second one, the Task suddenly stay in "Open" state and still sending warning email from the Task SLA

DizyM_0-1688979242295.png