- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 10:51 PM
Hi all,
I have requirement to manually close a SCTask that did not get cancelled when the RITM was "cancelled". Please assist with Background script that I can run to update the SCTask to cancelled state .
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 10:58 PM
You can use below logic:
var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addQuery('request_item','sys_id_of_ritm');
scTaskGr.addActiveQuery();
scTaskGr.query();
while(scTaskGr.next()){
scTaskGr.state = 4; // 4 is Closed Incomplete
scTaskGr.setWorkflow(false);
scTaskGr.update();
}
Replace sys_id of RITM in above script.
Make sure to put some logs and check which task is getting updated. Comment scTaskGr.update() while validating above script.
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 10:53 PM
Hi,
Can you please share more details?
How many task? is there any specific task to be cancelled?
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 10:58 PM
You can use below logic:
var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addQuery('request_item','sys_id_of_ritm');
scTaskGr.addActiveQuery();
scTaskGr.query();
while(scTaskGr.next()){
scTaskGr.state = 4; // 4 is Closed Incomplete
scTaskGr.setWorkflow(false);
scTaskGr.update();
}
Replace sys_id of RITM in above script.
Make sure to put some logs and check which task is getting updated. Comment scTaskGr.update() while validating above script.
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 10:59 PM - edited 03-28-2023 11:02 PM
Hello @JP6 ,
Please use the below script to close the task using background script :
var kids = new GlideRecord('sc_task');
kids.addQuery('parent', 'your RITM sys_id');
kids.query();
while (kids.next()) {
kids.state = 4;
kids.setWorkflow(false); // will not run the business after making the changes
kids.autoSysFields(false); // will not update the sys fields on the table
kids.update();
}
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 11:46 PM
You need to apply filters on "sc_task", as mentioned below screenshot:
and apply on the scripts by copying the query mentioned below:
var tasks = new GlideRecord("sc_task");
tasks.addEncodedQuery("request_item.state=4^stateIN1,8");
task.query();
while(tasks.next()){
tasks.state = 4;
tasks.setWorkflow(false);
tasks.update();
}
Please let me know if you need more information!
Please mark this answer as correct/helpful based on the impact 🙂