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

Ankur Bawiskar
Tera Patron
Tera Patron

@mcroxall 

the BR should be before update on sc_task

Condition: State [IS ONE OF] Closed Complete, Close Incomplete, Close Skipped

Script:

(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.addEncodedQuery('stateIN-5,1,2'); // Only active tasks
    scTask.addQuery('request_item', current.sys_id); // Tasks related to this request item
    scTask.setLimit(1);
    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);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@mcroxall 

your script is correct and the behavior is also OOB

when BR aborts it shows message and shows the same state value since it didn't allow the update in database.

User need to reload the record to see that the state has not changed

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@mcroxall 

try this once

(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.state = previous.state;
}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@mcroxall 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader