Understood! To implement your requirement using two different business rules in ServiceNow, we can follow these steps:
Business Rule 1: Set RITM and Remaining Tasks to Closed Incomplete
This business rule will handle the situation where any task is set to closed_incomplete. In this case, it will set all remaining active tasks to closed_incomplete and also set the RITM state accordingly.
Business Rule Configuration for Closed Incomplete:
- Name: Set RITM and Remaining Tasks to Closed Incomplete
- Table:
sc_task
- When: After
- Insert/Update: Check the Update option
- Condition:
current.state.changes() && current.state == 4 (this checks if the task state changed to closed_incomplete)
Script (in the Advanced tab):
// Check if the task's state has changed to closed_incomplete
if (current.state == 4) { // 4 is for 'closed_incomplete'
// Get the RITM record associated with this task
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
// Set the RITM state to closed_incomplete
ritm.state = 4; // 4 is for 'closed_incomplete'
ritm.update();
}
// Close all other active tasks related to this RITM
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.request_item);
taskGR.addQuery('sys_id', '!=', current.sys_id); // exclude the current task
taskGR.addQuery('active', true); // only check active tasks
taskGR.query();
while (taskGR.next()) {
taskGR.state = 4; // 4 is for 'closed_incomplete'
taskGR.update();
}
}
Business Rule 2: Set RITM to Completed
This second business rule will handle the completion of all tasks and set the RITM to completed only if all tasks are in the completed state.
Business Rule Configuration for Completed:
- Name: Set RITM to Completed
- Table:
sc_task
- When: After
- Insert/Update: Check the Update option
- Condition:
current.state.changes() && current.state == 3 (this checks if the task state changed to completed)
Script (in the Advanced tab):
// Check if the task has been marked as completed
if (current.state == 3) { // 3 is for 'completed'
// Get the RITM record associated with this task
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
// Check if all tasks related to this RITM are completed
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.request_item);
taskGR.addQuery('active', true); // only check active tasks
taskGR.query();
var allComplete = true;
while (taskGR.next()) {
if (taskGR.state != 3) { // If any task is not completed, allComplete becomes false
allComplete = false;
break; // No need to check further
}
}
// If all tasks are completed, set RITM to completed
if (allComplete) {
ritm.state = 3; // 3 is for 'completed'
ritm.update();
}
}
}
Explanation:
-
Business Rule for Closed Incomplete:
- This business rule activates when any task changes its state to closed_incomplete.
- It retrieves the associated RITM and updates its state to closed_incomplete.
- Then, it iterates through all other active tasks related to that RITM and sets their state to closed_incomplete.
-
Business Rule for Completed:
- This business rule triggers on task completion.
- It checks if the newly updated task is in the completed state.
- The script checks all active tasks related to the same RITM. If all tasks are found to be completed, it updates the RITM to completed.
Important Considerations:
- State Values: Ensure the numeric values are correctly associated with your ServiceNow instance (
4 for closed_incomplete and 3 for completed).
- Active Query: The queries include checks for active tasks just to ensure closed tasks are not processed unnecessarily.
- Testing: Test these business rules thoroughly in a development or test environment to confirm that both scenarios handle correctly and as expected.
Let me know if you need any additional assistance or modifications!