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

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

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

 

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

 

 

Hi @Avinash Uskamal . I created a workflow for my custom task table and I inserted it in SLA Definition

Request -> Task -> Task SLA  

 

DizyM_0-1688988676879.png

 

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