Request is still active even when the RITMs are closed

GBS
Tera Contributor

I want to close the active requests for which the RITMs are closed, complete or incomplete, using the background script. Can anyone please help me with the script

17 REPLIES 17

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @GBS 

 

You can do via list view as well, instead of doing via script.

*************************************************************************************************************
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/atul_grover_lng [ Connect for 1-1 Session]

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

Ankur Bawiskar
Tera Patron
Tera Patron

@GBS 

this will help, please enhance it further

closeRequest();

function closeRequest() {
    try {
        // Define the GlideRecord for the Request table
        var grRequest = new GlideRecord('sc_request');
        grRequest.addActiveQuery() // Only active requests
        grRequest.query();
        while (grRequest.next()) {
            // Define the GlideRecord for the RITM table
            var grRITM = new GlideRecord('sc_req_item');
            grRITM.addQuery('request', grRequest.sys_id);
            grRITM.addEncodedQuery('stateIN3,4,7'); // Closed, Complete, or Incomplete states
            grRITM.query();

            var allRITMsClosed = true;
            while (grRITM.next()) {
                if (grRITM.state != 3 && grRITM.state != 4 && grRITM.state != 7) {
                    allRITMsClosed = false;
                    break;
                }
            }

            if (allRITMsClosed) {
                grRequest.active = false;
                grRequest.state = 3; // Closed state
                grRequest.update();
                gs.info('Closed request: ' + grRequest.number);
            }
        }
    } catch (ex) {
        gs.info(ex);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar will this script close the RITMs which are open? 

@GBS 

nope

As per your question you asked to close open REQ if all RITMs under it are closed, so I shared the script accordingly

If my response helped please mark it correct and close the thread so that it benefits future readers.

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