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

@Ankur Bawiskar I have run the script in the background, but there are ritms for which the state is open also reflecting in the background script

@GBS 

please share your script here

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 

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.addActiveQuery(); // 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 

what are you trying to achieve?

the script I shared will work for your question asked

what didn't work as expected for the script I shared?

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

@Ankur Bawiskar I was trying to get the list of requests for which the ritms are closed complete via background script but it is showing the requests for which ritms are not in closed state