Restrict RITM Closure when CTASK is Open (Business Rule)

mcroxall
Tera Guru
Can someone help with this BR.  For some reason is not aborting the action and its saving the state change.2024-12-31_13-09-01.png

 What i am missing?



(
function executeRule(current, previous /*null when async*/) {

// Query to get all active tasks related to the current request item
var scTask = new GlideRecord('sc_task');
scTask.addActiveQuery(); // Only active tasks
scTask.addQuery('request_item', current.sys_id); // Tasks related to this request item
scTask.query();

// Check if there are any active tasks
if (scTask.hasNext()) {
// Add error message and prevent closing of request item
gs.addErrorMessage('Request Item cannot be closed until all related Tasks are closed.');
current.setAbortAction(true); // Prevent the current record from being updated
}

})(current, previous);
1 ACCEPTED SOLUTION

mcroxall
Tera Guru

i actually found that the script was taking in consideration when there were more than 1 task. so i update to script to: 

(function executeRule(current, previous /*null when async*/) {
// Query to get all tasks related to the current request item
var scTask = new GlideRecord('sc_task');
scTask.addQuery('request_item', current.sys_id); // Tasks related to this request item
scTask.addActiveQuery(); // Only active tasks
scTask.query();

// Flag to track active tasks
var hasActiveTasks = false;

// Loop through tasks to check for active ones
while (scTask.next()) {
hasActiveTasks = true; // Found at least one active task
gs.print("Active task found: " + scTask.number + " for RITM: " + current.number); // Debugging
break; // Exit loop early as we only need one active task to block the update
}

// If any active tasks exist, prevent the state change
if (hasActiveTasks) {
gs.addErrorMessage('Request Item cannot be closed until all related Tasks are closed.');
current.setAbortAction(true); // Prevent the current record from being updated
} else {
gs.print("No active tasks found for RITM: " + current.number); // Debugging
}
})(current, previous);
 

View solution in original post

5 REPLIES 5

mcroxall
Tera Guru

i actually found that the script was taking in consideration when there were more than 1 task. so i update to script to: 

(function executeRule(current, previous /*null when async*/) {
// Query to get all tasks related to the current request item
var scTask = new GlideRecord('sc_task');
scTask.addQuery('request_item', current.sys_id); // Tasks related to this request item
scTask.addActiveQuery(); // Only active tasks
scTask.query();

// Flag to track active tasks
var hasActiveTasks = false;

// Loop through tasks to check for active ones
while (scTask.next()) {
hasActiveTasks = true; // Found at least one active task
gs.print("Active task found: " + scTask.number + " for RITM: " + current.number); // Debugging
break; // Exit loop early as we only need one active task to block the update
}

// If any active tasks exist, prevent the state change
if (hasActiveTasks) {
gs.addErrorMessage('Request Item cannot be closed until all related Tasks are closed.');
current.setAbortAction(true); // Prevent the current record from being updated
} else {
gs.print("No active tasks found for RITM: " + current.number); // Debugging
}
})(current, previous);