After Business Rule is getting Executed before my flow is creating Task and closes the RITM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello,
This is my After update BR on SC Task Table
(function executeRule(current, previous /*null when async*/ ) {
if (current.request.state != '5') {
var ritmSysID = current.request_item.toString();
var grRitm = new GlideRecord('sc_req_item');
grRitm.query('sys_id', ritmSysID);
grRitm.query();
if (grRitm.next()) {
//Update RITM State
if (current.state == 2) { //Work in Progress
grRitm.state = 2;
grRitm.update();
} else if (current.state == 3 || current.state == 4) {
gs.sleep(10000);
if (grRitm.stage != 'waiting_for_approval') {
var sc_task = new GlideRecord('sc_task');
sc_task.addQuery('request_item', ritmSysID);
sc_task.addQuery('active', true);
sc_task.query();
if (!sc_task.next()) {
if (current.state == 3) { //Closed Complete
grRitm.state = 3;
} else if (current.state == 4) { //Closed Incomplete
grRitm.state = 4;
}
grRitm.update();
var reqSysID = grRitm.request.toString();
var grReq = new GlideRecord('sc_request');
grReq.query('sys_id', reqSysID);
grReq.query();
if (grReq.next()) {
if (grRitm.state == 3) { //Closed Complete
grReq.state = 3;
} else if (grRitm.state == 4) { //Closed Incomplete
grReq.state = 4;
}
grReq.update();
}
}
}
}
}
}
})(current, previous);
My flow is responsible for 2 Task and updating them for automating the state and stages we have created the above BR.
This works fine when i put a delay but as this is not recommended what can be done.
The issue is whenever my first task is gets closed this br runs before the flow creates the other task and RITM value is setting to close complete.
@Ankur Bawiskar any suggestions please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
you are using wrong variable name
try this
(function executeRule(current, previous /*null when async*/) {
if (current.request.state != '5') {
var ritmSysID = current.request_item + ''; // Stringify sys_id
var grRitm = new GlideRecord('sc_req_item');
if (!grRitm.get(ritmSysID)) return;
// WIP: Set immediately (safe)
if (current.state == 2) {
grRitm.state = 2;
grRitm.update();
return;
}
// Closed states (3=Complete, 4=Incomplete): Check ALL tasks first
if (current.state == 3 || current.state == 4) {
// Count ACTIVE tasks for this RITM - FIXED VARIABLE NAME
var task = new GlideRecord('sc_task');
task.addQuery('request_item', ritmSysID); // ← WAS ritmID, NOW ritmSysID
task.addQuery('active', true);
task.query();
var activeTaskCount = task.getRowCount();
if (activeTaskCount > 0) {
gs.info('Active tasks remain: ' + activeTaskCount + ' for RITM: ' + ritmSysID);
return;
}
// No active tasks - safe to close
grRitm.state = (current.state == 3) ? 3 : 4;
grRitm.update();
// Cascade to REQ if needed
var reqSysID = grRitm.request + '';
var grReq = new GlideRecord('sc_request');
if (grReq.get(reqSysID)) {
grReq.state = grRitm.state; // Use RITM state
grReq.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 || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Debasis Pati !!
This is a timing / race condition issue between your Flow and the After Update Business Rule on sc_task.
When the first task closes, the BR runs before the Flow has created the second task, so your query for active tasks returns none and the RITM is closed prematurely.
gs.sleep() only hides the issue and is not recommended.
Why this happens
Flow creates tasks asynchronously
After Update BR executes immediately on task closure
Second task does not yet exist when the BR runs
Recommended Fix (Best Practice)
Move the RITM/REQ state logic into the Flow, since the Flow owns task creation.
Flow pattern:
Create all sc_tasks
Wait until all related tasks are closed
Update RITM state/stage
Update REQ state
This avoids timing dependencies and is fully supported.
Alternative (If BR must be used)
Use a count-based check, not timing:
var ga = new GlideAggregate('sc_task');
ga.addQuery('request_item', current.request_item);
ga.addQuery('active', true);
ga.addAggregate('COUNT');
ga.query();
ga.next();
if (parseInt(ga.getAggregate('COUNT'), 10) === 0) {
// Close RITM and REQ
}This ensures the RITM is only closed when no active tasks exist, regardless of creation timing.
Mark this as Helpful if it clarifies the issue.
Accept the solution if this answers your question.
Regards,
Vaishnavi
Associate Technical Consultant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Debasis Pati,
I wish to give you a helping hand, but I am not understanding the requirement properly. You are using custom made flow to create two catalog tasks from a RITM? Then you want the RITM to be closed once the tasks have been closed as well?
Please clarify a bit, I'll assist you, I need to understand the requirement
