- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2023 10:48 PM
For one RITM, we built two sc_tasks. When I try to close one sc_task, RITM closes but another sc_task remains open. Can somebody assist me in closing all sc_tasks before RITM closes?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2023 11:04 PM
you can use after update BR on sc_task with proper conditions for State value
Condition: State [IS ONE OF] Closed complete, Closed incomplete, Closed skipped
Script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
// check if there is no active task for this RITM
var gr = new GlideRecord("sc_task");
gr.addActiveQuery();
gr.addQuery("request_item", current.request_item);
gr.setLimit(1);
gr.query();
if (!gr.hasNext()) {
var ritm = current.request_item.getRefRecord();
ritm.state = 3;
ritm.update();
}
})(current, previous);
OR
you can also use Flow designer with no script for this
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 12:54 AM
please use script I shared above.
sharing again with some changes
(function executeRule(current, previous /*null when async*/) {
// Add your code here
// check if there is no active task for this RITM
var gr = new GlideRecord("sc_task");
gr.addActiveQuery();
gr.addQuery("request_item", current.request_item);
gr.setLimit(1);
gr.query();
if (!gr.hasNext()) {
var ritm = current.request_item.getRefRecord();
ritm.state = current.state;
ritm.update();
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2023 10:57 PM
Hi
By creating a Business Rule on the sc_task table that triggers when a task is closed. In the Business Rule, you can check if all the related sc_tasks are closed, and if so, you can close the associated RITM.
Here is an example of a Business Rule script that you can use as a starting point:
// Get the current sc_task and its associated RITM
var currentTask = current;
var currentRITM = currentTask.request_item;
// Get all the sc_tasks associated with the RITM
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', currentRITM);
taskGr.query();
// Check if all the sc_tasks are closed
var allTasksClosed = true;
while (taskGr.next()) {
if (taskGr.getValue('state') != '7') { // '7' is the state value for Closed
allTasksClosed = false;
break;
}
}
// If all sc_tasks are closed, close the associated RITM
if (allTasksClosed) {
currentRITM.setValue('state', 3); // '3' is the state value for Closed Complete
currentRITM.update();
}
Thanks,
Rahul Kumar
Thanks,
Rahul Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2023 11:04 PM
you can use after update BR on sc_task with proper conditions for State value
Condition: State [IS ONE OF] Closed complete, Closed incomplete, Closed skipped
Script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
// check if there is no active task for this RITM
var gr = new GlideRecord("sc_task");
gr.addActiveQuery();
gr.addQuery("request_item", current.request_item);
gr.setLimit(1);
gr.query();
if (!gr.hasNext()) {
var ritm = current.request_item.getRefRecord();
ritm.state = 3;
ritm.update();
}
})(current, previous);
OR
you can also use Flow designer with no script for this
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 12:51 AM
we are using this script is it correct.
(function executeRule(current, previous /*null when async*/) {
var gr=new GlideRecord('sc_task');
gr.addQuery('request_item',current.request_item);
gr.addQuery('state', '!=',current.state);//checking if all tasks are closed
if(!gr.next())
{
var grr = new GlideRecord('sc_req_item');
grr.get(current.request_item);
grr.state=current.state;
grr.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 12:54 AM
please use script I shared above.
sharing again with some changes
(function executeRule(current, previous /*null when async*/) {
// Add your code here
// check if there is no active task for this RITM
var gr = new GlideRecord("sc_task");
gr.addActiveQuery();
gr.addQuery("request_item", current.request_item);
gr.setLimit(1);
gr.query();
if (!gr.hasNext()) {
var ritm = current.request_item.getRefRecord();
ritm.state = current.state;
ritm.update();
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader