Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2024 11:09 AM
Can someone help with this BR. For some reason is not aborting the action and its saving the state change.
(function executeRule(current, previous /*null when async*/) {
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);
Solved! Go to Solution.
1 ACCEPTED SOLUTION
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 01:05 PM
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);
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 01:05 PM
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);