- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi All
I have a task that RITM should stay in Open state until all the tasks will be closed.
Tasks are mainly created by workflow but there is also possibility to create task manually from RITM.
Workflow automatically close the RITM when all of the workflow tasks are closed.
So I've created a Before Update Business Rule on sc_req_item table:
Condition: current.state.changesTo('5') // 5 - Fulfillment Completed
Script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
don't close RITM from Workflow
My thoughts
-> do not close the RITM while its workflow context is still executing
Update your sc_task After Update BR as this
(function executeRule(current, previous) {
if (current.request_item.nil()) {
return;
}
var ritmId = current.request_item.toString();
// 1. Are there any active catalog tasks still open?
var openTask = new GlideRecord('sc_task');
openTask.addQuery('request_item', ritmId);
openTask.addActiveQuery();
openTask.setLimit(1);
openTask.query();
if (openTask.hasNext()) {
return;
}
// 2. Is workflow still running for this RITM?
var wf = new GlideRecord('wf_context');
wf.addQuery('id', ritmId);
wf.addQuery('table', 'sc_req_item');
wf.addQuery('state', 'IN', 'executing,waiting');
wf.setLimit(1);
wf.query();
if (wf.hasNext()) {
return;
}
// 3. Safe to close RITM
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(ritmId)) {
if (ritm.state != 5) {
ritm.state = 5;
ritm.stage = 'fulfillment_complete';
ritm.u_fulfilled_at = gs.nowDateTime();
ritm.active = false;
ritm.update();
}
}
})(current, previous);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
don't close RITM from Workflow
My thoughts
-> do not close the RITM while its workflow context is still executing
Update your sc_task After Update BR as this
(function executeRule(current, previous) {
if (current.request_item.nil()) {
return;
}
var ritmId = current.request_item.toString();
// 1. Are there any active catalog tasks still open?
var openTask = new GlideRecord('sc_task');
openTask.addQuery('request_item', ritmId);
openTask.addActiveQuery();
openTask.setLimit(1);
openTask.query();
if (openTask.hasNext()) {
return;
}
// 2. Is workflow still running for this RITM?
var wf = new GlideRecord('wf_context');
wf.addQuery('id', ritmId);
wf.addQuery('table', 'sc_req_item');
wf.addQuery('state', 'IN', 'executing,waiting');
wf.setLimit(1);
wf.query();
if (wf.hasNext()) {
return;
}
// 3. Safe to close RITM
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(ritmId)) {
if (ritm.state != 5) {
ritm.state = 5;
ritm.stage = 'fulfillment_complete';
ritm.u_fulfilled_at = gs.nowDateTime();
ritm.active = false;
ritm.update();
}
}
})(current, previous);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Works perfect, thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi,
I think this issue is happening because your second Business Rule checks only for currently open tasks at the moment a task is closed, but the workflow may still create new workflow tasks immediately after that. So when the manual task is closed, the BR thinks all tasks are completed and closes the RITM before the next workflow task gets generated. Instead of directly closing the RITM from the sc_task BR, you should also validate that the workflow itself is completed (or add a small delayed check using event/async logic).
A better approach would be to query all active sc_task records for the RITM after workflow execution is fully finished, rather than checking immediately on task closure. This avoids the race condition where new workflow tasks are created after your validation runs.
