Autoclose ritm and request when all sctask closes

runda
Tera Contributor

Hi 

 

I have written a after business rule on sc_task table with condition as state is closed complete to autoclose ritm and requests but it is not checking for all the open sctasks from the flow and still the requests get closed

 

(function executeRule(current, previous /*null when async*/) {
// Add your code here
var tasks = new GlideRecord('sc_task');
//tasks.addQuery('state', 'IN','3,4,7');
//tasks.addQuery('request_item.state','NOT IN','3,4,7');
tasks.addQuery('request_item',current.request_item);
tasks.addEncodedQuery('state=1^ORstate=2');
tasks.addActiveQuery();
//tasks.addQuery('state', '!=' , '3');
tasks.query();
if(tasks.next())
{
gs.addErrorMessage('There are open task records for this requested item. Please close all tasks.');
current.state=previous.state;
current.setAbortAction(true);
} else {
var rq = new GlideRecord('sc_req_item');
rq.addQuery('sys_id',current.request_item);
rq.query();
if(rq.next())
{
rq.state = 3;
rq.update();
}
}
var req = new GlideRecord('sc_request');
{ req.addQuery('sys_id', current.request);
req.query();
if(req.next())
{
req.state = 3; // check the state in your environment
req.request_state = 'closed_complete'; // check the state in your environment
req.update();
}
}})(current, previous);

 

The business rule doesnt wait for the flow to close all tasks generated

Please help

6 REPLIES 6

AnubhavRitolia
Mega Sage
Mega Sage

Hi @runda 

 

If you are already have a Flow defined for your Requested Item process, it would be more recommended to achieve this also through flow instead of Business Rule.

 

After your flow closed all SC Tasks, than you check if all sc_tasks are closed for current RITM or not and accordingly update the state values of RITM and REQ.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

But how can I add flow to all catlog items as we are creating many catlog items business rule will be global

 

DaniJ06
Tera Contributor

Just wondering did you ever get a solution for this issue and if you could share it, please? 

Brian Lancaster
Tera Sage

I haven't used flow yet for catalog items. I would think it works similar to workflow were once all the catalog tasks are closed you would set the state and stage of the ritm then out of the box business rules take care of the rest.