Looking for help on using ad hoc tasks with workflow tasks and RITM closes after last task is closed

Julie Catano
Kilo Guru

We use Service Catalog which creates RITM and catalog tasks. When the last WF task completes, the WF closes the RITM. That's all good. But now we are going to let users create ad-hoc tasks manually on an RITM. That means when the last WF task closes it will close the RITM and orphan the manually added tasks.  

 

I have created two business rules, one that waits until the last task is closed based on the workflow.  But if there is an ad hoc task then the RITM closes and leave the ad hoc task as an orphan.  So I reached out to the community and got a BR to look at ALL tasks and close RITM when they are close complete, closed incomplete, closed skipped. It had been working but once I // commented out the message things seemed to not work.

 

What I'm looking for is something that will check and if there is an ad hoc task added and not close the RITM until all tasks (even workflow tasks) are close complete, closed incomplete, closed skipped.  

 

I do have workflow setup value to make Stage to complete (see screen shot).

 

My end result that I am looking to accomplish is once ALL tasks are close complete, closed incomplete, closed skipped (ad hoc and workflow), then the RITM needs to change from State = close complete and Stage = completed.

 

BR #1 - Prevent RITM to close when task it open, this works with workflow but not an ad hoc task.

(function executeRule(current, previous /*null when async*/) {
 
// Add your code here
var SC_task = new GlideRecord('sc_task');
SC_task.addQuery('active', true);
SC_task.addQuery('request_item', current.sys_id);
SC_task.query();
if(SC_task.next()){
 
gs.addInfoMessage('your task is not closed please close them first');
current.state = previous.state;
current.setAbortAction('true');
 
}
})(current, previous);
 
BR#2 - Don't close RITM until ALL tasks are closed even an ad hoc task.  See screen shot


(function executeRule(current, previous /*null when async*/) {
// Query to check if there are any open sc_tasks for the current ritm
var task = new GlideRecord('sc_task');
task.addQuery('request_item', current.request_item);
task.addQuery('active', true);
task.query();

var hasActiveTasks = false;

// Check if there are any active tasks
while (task.next()) {
hasActiveTasks = true;
break;
}

if (!hasActiveTasks) {
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
// Query to check if there are any active tasks remaining for the current ritm
var remainingTasks = new GlideRecord('sc_task');
remainingTasks.addQuery('request_item', current.request_item);
remainingTasks.addQuery('active', true);
remainingTasks.query();

var activeTaskCount = 0;
while (remainingTasks.next()) {
activeTaskCount++;
}

// If no active tasks are remaining, close the ritm
if (activeTaskCount === 0) {
ritm.state = 3; // Closed state
ritm.update();
}
}
}
})(current, previous);

1 REPLY 1

GChanner
Tera Guru

Hi Julie,

 

I have a similar use-case and ran into the same issues of RITM not closing. Did you ever get this resolved?

 

Thanks

Garfield