Looking for help with a business rule that will change "stage" to "completed"

Julie Catano
Kilo Guru

I have a business rule (see below) that when all tasks are closed completed then it will close the RITM even when the request is using workflow and an ad hoc task was created. This code is working for when the last SCTASK is closed then it closes the RITM. 

 

The issue is that it's NOT changing the "Stage" to "Completed" It's only changing the "State" to "Close Complete", and I need them both to show as completed.  See screenshot below.


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

 

10 REPLIES 10

So there is no way for the code to look at ALL tasks and when ALL tasks have been completed from either an ad hoc task or a workflow task then once all tasks are completed then change the RITM to state=close complete and stage=completed?  I thought out of the box you could add an ad hoc task even if the catalog item has workflow with multiple tasks.

there is , but it cannot look at future tasks

 

What I mean is::

Lets say on the first step of the workflow you create 2 tasks.

Then these tasks are closed(both), at tnis time yiur BR will see that all tasks for this RITM are closed and close the RITM.

On the Third step there is another task created -> this is an issue in your logic as RITM is closed already.

 

Only way to get past this is in your workflow, on the very last step wait for all tasks to close and then proceed to close the RITM, what you are gaining here is that there will not be any more tasks created for this, its all already created.

-Anurag

Julie Catano
Kilo Guru

I added the wait condition in the workflow along with the above BR and the RITM is still changing the state to Closed Complete.  See screen shots below.

Remove the BR, deactivate it.

 

Your wait for condition should wait for all tasks to close and in the next activity you set state and stage both to complete.

 

-Anurag

I deactivated BR but when I go to the "wait" condition in there is no "catalog task" in drop down menu to specify the condition. See screen shot.