How to close sctasks if related RITM is getting cancelled?

nirajjoshi
Tera Contributor

To automatically close scheduled tasks (sctasks) in ServiceNow when the related Requested Item (RITM) is getting canceled, you can achieve this through Business Rules or Script Actions. Here's a basic outline of how you can implement this:

1. Identify the Relationship: Ensure that there's a clear relationship between the RITM and the scheduled tasks. This might involve a field on the scheduled task that links it to the RITM, such as a reference field.

2. Create a Business Rule or Script Action: You'll need to create a Business Rule or Script Action that listens for the cancellation of RITM records.

3. Check Related sctasks: In the Business Rule or Script Action, query for any related scheduled tasks associated with the cancelled RITM.

4. Close sctasks: Iterate through the related scheduled tasks and close them.

Here's an example of how you might implement this in a Business Rule:

add below script in Business rule in advanced section
(function executeRule(current, previous /*null when async*/) {

// Query for related scheduled tasks
var sctasks = new GlideRecord('sc_task');
sctasks.addQuery('request_item', current.sys_id); // Assuming 'request_item' is the reference field linking RITM & //sctasks
sctasks.query();

// Close all/multiple related scheduled task
while (sctasks.next()) {
sctasks.state = 3; // Set state to 'Closed'
sctasks.update();
}

//closing any one related scheduled task

/* if(ritm1.next())
{
    ritm1.state=current.state;
    ritm1.update();
  }*/

})(current, previous);

In this script:

- We assume 'request_item' is the reference field on the `sc_task` table that links it to the RITM.
- We query for all scheduled tasks related to the canceled RITM.
- We then iterate through each found task and set its state to 'Closed'.

Make sure to adapt this script to your specific field names and requirements.

Remember to thoroughly test any changes in a non-production environment before implementing them in your live ServiceNow instance. Additionally, consider any potential impacts on existing processes before deploying changes like this.

0 REPLIES 0