Help! Business Rule to set RITM state based on Catalog tasks states
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 08:12 AM
Hi Community,
I have an After update business rule on the sc_task table to set the RITM state. I am having an issue where my logic is NOT waiting for other generated catalog tasks before setting RITM state. I have path where I have several catalog tasks generated one after the other. When my first catalog task is marked Closed Complete and the second catalog task is inserted the RITM state is already marked Closed Complete. My guess both are happening at the same time or the BR is running a few seconds before the workflow?
My BR and section of workflow is copied below...
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', current.request_item);
ritm.query();
if (ritm.next()) {
var act = 0;
var cancel = 0;
var comp = 0;
var pending = 0;
var open = 0;
var skip = 0;
var wip = 0;
var sc = new GlideRecord('sc_task');
sc.addQuery('request_item', ritm.sys_id);
sc.query();
while (sc.next()) {
if (sc.active)
act++;
if (sc.state == '3') //Closed completed
comp++;
else if (sc.state == '-5') //pending
pending++;
else if (sc.state == '7') //Closed skipped
skip++;
else if (sc.state == '1') //Open
open++;
else if (sc.state == '4') //Closed Incomplete
cancel++;
else
wip++;
}
if (comp == sc.getRowCount()) {
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('state', 'requested');
appr.addQuery('document_id', ritm.sys_id);
appr.query();
if (!appr.next()) {
ritm.state = '3'; //closed complete
ritm.stage = 'complete';
ritm.active = false;
ritm.update();
} else {
ritm.state = '2';
ritm.stage = 'waiting_for_approval';
ritm.update();
}
} else {
if (cancel == sc.getRowCount()) {
// ritm.setWorkflow(false);
var flows = new Workflow().getRunningFlows(ritm);
if (flows.next()) {
new Workflow().cancelContext(flows);
}
ritm.state = '4'; //Closed Incomplete
//ritm.stage = 'complete';
ritm.active = false;
ritm.update();
} else {
if (skip == sc.getRowCount()) {
ritm.state = '7';
//ritm.stage = 'complete';
ritm.active = false;
ritm.update();
} else {
var apprv = new GlideRecord('sysapproval_approver');
apprv.addQuery('state', 'requested');
apprv.addQuery('document_id', ritm.sys_id);
apprv.query();
if (!apprv.next()) {
if (pending == sc.getRowCount() || (pending > 0 && wip == 0)) {
ritm.state = '-5';
ritm.update();
}
if (wip > 0 && pending == 0) {
ritm.state = '2';
ritm.update();
}
if (wip > 0 && pending > 0) {
ritm.state = '-5';
ritm.work_notes = 'Requested item is in Pending state';
ritm.update();
}
if (skip == sc.getRowCount()) {
ritm.state = '4';
ritm.update();
}
if (comp == sc.getRowCount() || ((parseInt(skip) + parseInt(comp)) == sc.getRowCount())) {
ritm.state = '3';
ritm.update();
}
} else {
ritm.state = '2';
ritm.stage = 'waiting_for_approval';
ritm.update();
}
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 08:17 AM
Your After Update Business Rule on sc_task is setting the sc_req_item (RITM) state before all catalog tasks are completed. This happens because your script runs every time a task updates, without waiting for all tasks to be created.
Please mark it helpful
Regards
Priyatam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 10:41 AM
Try adding a 5 second delay at the beginning of the Business Rule script.
var nowdt = new GlideDateTime();
while (GlideDateTime.subtract(nowdt, new GlideDateTime()) < "1970-01-01 00:00:05") {
//5 second loop
}
There may be other issues if you're not getting the expected catalog task count for your test case. You can add some logs to the BR script to determine this.