Parent REQs need to be closed when the child RITMs are not open

divyadhanda
Tera Expert

I want to write a fix script to update the parent Req is open when child RITMs/Sctasks are not open (Cancelled, Closed incomplete, closed complete).

4 REPLIES 4

Dr Atul G- LNG
Tera Patron

Hi @divyadhanda 

Sorry, I didn’t quite understand this. When the RITM, SCTASK, not open and REQ are all in the Open state, is that expected behavior, or am I missing something here? Could you please provide more details?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron

@divyadhanda 

you want to do this for older REQs?

If yes then here is your fix script

note: please test in DEV and TEST before running in PROD

(function() {
    var req = new GlideRecord('sc_request');
    req.addQuery('state', '!=', 1); // Not already open
    req.query();
    
    var count = 0;
    while (req.next()) {
        // Check if ALL RITMs AND SCTASKs are closed (state 3,4,7)
        var hasOpenRitm = false;
        var ritm = new GlideRecord('sc_req_item');
        ritm.addQuery('request', req.sys_id);
        ritm.addQuery('state', 'NOT IN', '3,4,7');
        ritm.query();
        if (ritm.next()) hasOpenRitm = true;
        
        var hasOpenTask = false;
        var task = new GlideRecord('sc_task');
        task.addQuery('request', req.sys_id);
        task.addQuery('state', 'NOT IN', '3,4,7');
        task.query();
        if (task.next()) hasOpenTask = true;
        
        // Reopen if no open children
        if (!hasOpenRitm && !hasOpenTask) {
            req.state = 1; // Open
            req.update();
            count++;
            gs.info('Reopened: ' + req.number);
        }
    }
    gs.info('Updated ' + count + ' requests');
})();

Also going forward you can write After update BR on sc_task to close RITM and REQ when last scTask is closed

After Update: sc_task

Condition: State [Changes To] Closed Complete/Closed Incomplete/Close Skipped

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.addQuery('active', true);
    gr.query();
    if (!gr.next()) {

        // close RITM
        var ritm = current.request_item.getRefRecord();
        ritm.state = 3;
        ritm.update();

        // close REQ
        var req = current.request.getRefRecord();
        req.state = 3;
        req.update();
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

But i want to fetch only the open REQs whose child RITMs/Sctasks are not open, 

 

For those open req's. I need to update as Request.state=close

Please check and let me know if this is correct.
var req = new GlideRecord('sc_request');
req.addQuery('active', true);   
req.addQuery('state', 1);      
req.query();
while (req.next()) {
      var ritm = new GlideAggregate('sc_req_item');
    ritm.addQuery('request', req.sys_id);
    ritm.addQuery('state', 1);  
    ritm.addAggregate('COUNT');
    ritm.query();
    if (ritm.next() && ritm.getAggregate('COUNT') > 0)
        continue;
   
    var task = new GlideAggregate('sc_task');
    task.addQuery('request_item.request', req.sys_id);
    task.addQuery('state', 1);  // SCTASK Open
    task.addAggregate('COUNT');
    task.query();
    if (task.next() && task.getAggregate('COUNT') > 0)
        continue;
 
    gs.print('REQ eligible to close: ' + req.number);
}