in servicenow, i want that if all catalog task associated to my RITM gets closed then RITM and Req also gets closed , and if any tasks gets closed incomplete then immediately other tasks and RITM gets closed incomplete, how to do it with the help of business rule, p
Assistant
User
how to do it in one business rule, that if any task gets closed incomplete then all the tasks gets incomplete too
Assistant
To implement this functionality in ServiceNow where you're managing the behavior of Requested Item (RITM) and its associated catalog tasks based on their states, you would need to create a couple of Business Rules. The following is a guideline on how to accomplish this:
1. Closing the RITM and Request when all tasks are closed
Business Rule: Close RITM when all tasks are closed
var ritu = new GlideRecord('sc_req_item');
if (ritu.get(current.request_item)) {
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', ritu.sys_id);
taskGr.addQuery('state', '!=', 'Closed Complete');
taskGr.query();
if (!taskGr.hasNext()) { // If there are no incomplete tasks
ritu.state = 'Closed Complete';
ritu.update();
// Close the associated request
var reqGr = new GlideRecord('sc_request');
if (reqGr.get(ritu.request)) {
reqGr.state = 'Closed Complete';
reqGr.update();
}
}
}
This business rule checks whether any of the associated catalog tasks of the current RITM is not closed. If none are “Closed Complete,” it sets the state of the RITM and the associated request to “Closed Complete”.
2. Closing RITM and tasks when any task is closed incomplete
Business Rule: Close RITM and tasks when any task is closed incomplete
Name: Close RITM and tasks when any task is closed incomplete
var ritu = new GlideRecord('sc_req_item');
if (ritu.get(current.request_item)) {
// Close the RITM
ritu.state = 'Closed Incomplete';
ritu.update();
// Close associated request
var reqGr = new GlideRecord('sc_request');
if (reqGr.get(ritu.request)) {
reqGr.state = 'Closed Incomplete';
reqGr.update();
}
// Close other related tasks
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', ritu.sys_id);
taskGr.addQuery('sys_id', '!=', current.sys_id);
taskGr.query();
while (taskGr.next()) {
taskGr.state = 'Closed Incomplete';
taskGr.update();
}
}
This business rule will close the RITM and its related request when any task is closed as "Closed Incomplete." It also updates all other tasks associated with the RITM to "Closed Incomplete."
Additional Considerations
Ensure you have the right state values configured in your instance. The script assumes "Closed Complete" and "Closed Incomplete" are valid states.
Test the business rules in a sub-production environment before moving them to production to ensure they work as expected.
Depending on your instance's performance and volume of tasks, some optimizations or checks may be needed to avoid excessive database operations.
This approach allows for smooth automation of task and RITM states based on the conditions you specified.
To implement a requirement in ServiceNow where closing any task as "Closed Incomplete" also leads to closing all associated tasks and the RITM as "Closed Incomplete," you can accomplish this with a single Business Rule. Here’s how you can set it up:
Business Rule: Close All Tasks and RITM When Any Task is Closed Incomplete
Name: Close All Tasks and RITM When Any Task is Closed Incomplete
// Get the RITM (Request Item) record associated with the current task
var ritu = new GlideRecord('sc_req_item');
if (ritu.get(current.request_item)) {
// Close the RITM as "Closed Incomplete"
ritu.state = 'Closed Incomplete';
ritu.update();
// Close all other tasks associated with this RITM
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', ritu.sys_id);
taskGr.addQuery('sys_id', '!=', current.sys_id); // Exclude the current task
taskGr.query();
while (taskGr.next()) {
taskGr.state = 'Closed Incomplete';
taskGr.update();
}
}
Explanation of the Script:
RITM Lookup: The GlideRecord is used to find the RITM associated with the catalog task being updated. It retrieves the RITM record using the current.request_item field.
Update RITM State: Once the RITM is found, its state is updated to "Closed Incomplete."
Close Associated Tasks: The script then executes another GlideRecord query to gather all catalog tasks that are associated with the same RITM (request_item) but skips the current task that triggered this Business Rule. It updates their state to "Closed Incomplete."
Additional Considerations
Testing: Be sure to test this Business Rule in a development or test environment to ensure that it behaves as expected in various scenarios.
Performance Impact: If there are many catalog tasks associated with a RITM, make sure that this processing is performed efficiently to avoid any noticeable performance degradation.
Valid State Values: Ensure that your instance uses "Closed Incomplete" as a valid state value. If your instance uses different values, you’ll need to adapt the script accordingly.
Error Handling: Depending on your environment and needs, consider adding error handling (e.g., check if update operations are successful) based on the organizational standards.
This setup should efficiently handle the requirement to close all tasks and the RITM when any individual task is marked as closed incomplete within a single Business Rule.