How to stop an active workflow if it's associated RITM is closed

Alfredo Lopez1
Tera Expert

Analysts are closing RITM's without closing the related Catalog tasks and the record remains in the Workflow contexts as active=true and state=executing. I created a business rule to prevent the analysts to close future RITMs without closing the respective task but now I need to go back to the previous RITM's and set the workflow to active=false and state=finished for all live workflows showing in the active context list.

I created a fix script and this is what I have so far - I'm having issues to query for the closed RITM in order to update the WF context. Please assist to fix this records.

 

// Look for WF records that have associated RITM records
// where the WF records are "active and executing"
var gr = new GlideRecord('wf_context');
gr.addEncodedQuery('active=true^state=executing');

// Query and change the output results
gr.query();
if(gr.next()){
gr.sc_req_item.current.wf.id.state="Closed Complete";
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.setState = "Finished";
gr.update();
}

 

1 ACCEPTED SOLUTION

Mike,

Thank you for your help, after trial and error, I'm going to use the following and I added the gr.active = "false"; at the end

 

// Look for WF records that have associated RITM records
// where the WF records are "active and executing"
// and the RITM record is not "active"
var gr = new GlideRecord('wf_context');
gr.addEncodedQuery('active=true^state=executing');

// Query and change the WF record to "false and Finished"
gr.query();
while(gr.next()){
if(gr.id.active==false){

gr.autoSysFields(false);
gr.setWorkflow(false);
gr.state = "Finished";
gr.active = "false";
gr.update();
}
}

View solution in original post

2 REPLIES 2

Mike Patel
Tera Sage

try below

var gr = new GlideRecord('wf_context');
gr.addEncodedQuery('active=true^state=executing');
gr.query();
while(gr.next()){ 
	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('sys_id', gr.id);
	ritm.query();
	if(ritm.next()){
		if(ritm.state == 'Close Complete'){
			gr.autoSysFields(false);
			gr.setWorkflow(false);
			gr.setState = "Finished";
			gr.update();
		}
	}
}

Mike,

Thank you for your help, after trial and error, I'm going to use the following and I added the gr.active = "false"; at the end

 

// Look for WF records that have associated RITM records
// where the WF records are "active and executing"
// and the RITM record is not "active"
var gr = new GlideRecord('wf_context');
gr.addEncodedQuery('active=true^state=executing');

// Query and change the WF record to "false and Finished"
gr.query();
while(gr.next()){
if(gr.id.active==false){

gr.autoSysFields(false);
gr.setWorkflow(false);
gr.state = "Finished";
gr.active = "false";
gr.update();
}
}