How to stop a workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2023 11:12 PM - edited 07-09-2023 11:13 PM
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:
Workflow:
Thank you in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2023 11:26 PM
When: After
Insert: True
Update: True
Condition: current.active.changesTo(false)
new Workflow().cancel(current); //add to script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 01:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2023 11:29 PM - edited 07-09-2023 11:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 01:04 AM - edited 07-10-2023 01:54 AM
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