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

@GBS 

seems there might be issue with the active flag during REQ query

try this in query

 grRequest.addEncodedQuery('stateIN-5,1,2');

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 Using the below script, even though it is not working

closeRequest();

function closeRequest() {
    try {
        var totalRequestsProcessed = 0; // Counter for total requests processed
        var totalRequestsClosed = 0;    // Counter for total requests closed

        // Define the GlideRecord for the Request table
        var grRequest = new GlideRecord('sc_request');
        grRequest.addEncodedQuery('stateIN-5,1,2'); // Only active requests
        grRequest.query();

        while (grRequest.next()) {
            totalRequestsProcessed++; // Increment processed count

            // 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; // Flag to check if all RITMs are closed
            while (grRITM.next()) {
                if (grRITM.state != 3 && grRITM.state != 4 && grRITM.state != 7) {
                    allRITMsClosed = false;
                    break;
                }
            }

            if (allRITMsClosed) {
                // Uncomment the following lines to actually close the requests
                // grRequest.active = false;
                // grRequest.state = 3; // Closed state
                // grRequest.update();
               
                totalRequestsClosed++; // Increment closed count
                gs.info('Ready to close request: ' + grRequest.number); // Log the request number
            }
        }

        // Log summary
        gs.info('Total requests processed: ' + totalRequestsProcessed);
        gs.info('Total requests ready for closure: ' + totalRequestsClosed);

    } catch (ex) {
        gs.error('Error occurred: ' + ex.message);
    }
}

@GBS 

seems some of your RITMs might be in Open, Pending or Work In progress

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

@Ankur Bawiskar that's what my requirement is, some ritms are closed complete and the same request is still active even when the RITM is closed complete. 

@GBS 

so my script will close the REQs for which all RITMs are closed

If any 1 RITM under that active REQ is not closed it won't close the REQ

Script I shared will solve your requirement.

I am not getting what's not working as per my script.

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