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-10-2023 01:41 AM - edited 07-10-2023 01:43 AM
Hi @Dizy M ,
Please try workflow api methods in this document and put logs before cancelling any workflow.
https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_Workflow_Workflow
Regards, Anup
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 02:05 AM
Hi @Dizy M ,
on which table you have created workflow, if it is request item then it will not work.
please provide more input.
Thanks
Avinash Uskamalla
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 03:04 AM
hi @Dizy M
if your workflow is written on request item,then you need to write two after business rule.
1.request table(sc_req)
update
condition:state to closed cancelled
var gr=new GlideRecord("sc_req_item");
gr.addQuery("request",current.sys_id);
gr.query();
current.state="4";
current.update();
2.request item table(sc_req_item)
update
condition:state to closed incomplete
script:
var flows = new Workflow().getRunningFlows(current);
while(flows.next()){
//Check for associated workflows by name
if(flows.workflow_version.getDisplayValue() == 'ritmmmcancel'){//name of the workflow
//Cancel the workflow context
new Workflow().cancelContext(flows);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 04:32 AM
Hi @Avinash Uskamal . I created a workflow for my custom task table and I inserted it in SLA Definition
Request -> Task -> Task SLA
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 04:40 AM
@Avinash Uskamal -- What happened was when the user tried to cancel the request using the "Cancel" Button in the portal form, the workflow is not stopping, it keeps sending warning email( this warning email is on the workflow).
This is the BR for the "Request" table :
After Update
Condition: State changes to cancelled
(function executeRule(current, previous /*null when async*/) {
var Task = new GlideRecord('custom_task_table');
Task.addQuery('parent_request', current.sys_id);
Task.query();
while (Task.next()) {
Task.state = 6; // Cancelled
Task.active = false;
Task.setWorkflow(false);
Task.update();
var taskSysID = Task.sys_id;
var gr1 = new GlideRecord("task_sla");
gr1.addQuery('task.sys_id', taskSysID);
gr1.addEncodedQuery("active=true");
gr1.query();
if (gr1.next()) {
gr1.active = 'false';
gr1.stage ='cancelled';
gr1.end_time = new GlideDateTime();
gr1.update();
}
}
})(current, previous);
Below is the BR for our custom task table:
After Insert, Update
Condition: State changes to Cancelled
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('wf_context');
gr.addEncodedQuery('active=true^state=executing');
gr.query();
while(gr.next()){
var task = new GlideRecord('custom_task_table');
task.addQuery('sys_id', gr.id);
task.query();
if(task.next()){
if(task.state == 'Cancelled'){
gr.active = "false";
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.setState = "Cancelled";
gr.update();
}
}
}
})(current, previous);