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

Amit Gujarathi
Giga Sage
Giga Sage

HI @runda ,
I trust you are doing great.
Here is a modified version of your script with added comments for clarity:

(function executeRule(current, previous /*null when async*/) {
    // Query to find related tasks
    var tasks = new GlideRecord('sc_task');
    tasks.addQuery('request_item', current.request_item);
    tasks.addEncodedQuery('state=1^ORstate=2'); // Adjust the state values as per your requirement
    tasks.addActiveQuery();
    tasks.query();

    if (tasks.hasNext()) { // Checks if there are active tasks
        gs.addErrorMessage('There are open task records for this requested item. Please close all tasks.');
        current.state = previous.state;
        current.setAbortAction(true);
        return; // Exit if there are active tasks
    }

    // If no active tasks, proceed to close the request item
    var ritm = new GlideRecord('sc_req_item');
    ritm.get(current.request_item);
    if (ritm.isValidRecord()) {
        ritm.state = 3; // Update the state as per your instance's configuration
        ritm.update();
    }

    // Close the associated request
    var req = new GlideRecord('sc_request');
    req.get(ritm.request);
    if (req.isValidRecord()) {
        req.state = 3; // Update the state as per your instance's configuration
        req.request_state = 'closed_complete'; // Update the request state as needed
        req.update();
    }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



This is helpful, but is there any way to accomplish this without a script, maybe via the flow designer I have created something, but it does not wait for all associated tasks to close before it closes the RITM/REQ. We have the add to cart function and order guides that end up making one REQ and multiple RITM and SC Tasks no matter what rule I add it still ends up closing the REQ once one SC Task is closed. Same goes for one REQ one RITM and multiple SC Tasks. 

Thanks ahead of time!