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 

Hope you are doing good.

Did my reply answer your question?

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 Hi Ankur, I'm using the script below to set the request status to inactive, but when I run the script in the background, it shows the same count for the Total processed requests and for the Total requests that would have been closed. 

closeRequest();

function closeRequest() {
    try {
        var totalRequestsProcessed = 0;
        var totalRequestsClosed = 0;

       
        var grRequest = new GlideRecord('sc_request');
        grRequest.addActiveQuery();
        grRequest.query();
        var totalRequests = grRequest.getRowCount();
        gs.info('Total active requests to process: ' + totalRequests);

        while (grRequest.next()) {
            gs.info('Processing Request: ' + grRequest.number);
            totalRequestsProcessed++;

            var grRITM = new GlideRecord('sc_req_item');
            grRITM.addQuery('request', grRequest.sys_id);
            grRITM.addEncodedQuery('stateIN3,4,7');
            grRITM.addQuery('cat_item.category', '!=', 'order_guide');
            grRITM.query();

            var allRITMsClosed = true;
            while (grRITM.next()) {
                gs.info('  RITM: ' + grRITM.number + ', State: ' + grRITM.state);
                if (grRITM.state != 3 && grRITM.state != 4 && grRITM.state != 7) {
                    allRITMsClosed = false;
                    gs.info('  RITM ' + grRITM.number + ' is not in a closed, complete, or incomplete state.');
                    break;
                }
            }

            if (allRITMsClosed) {
                gs.info('  All RITMs are in a valid state for Request: ' + grRequest.number);
                totalRequestsClosed++;
                // Commenting the actual closing of the request for now
                // grRequest.active = false;
                // grRequest.update();
                gs.info('  Would have closed Request: ' + grRequest.number);
            } else {
                gs.info('  Request ' + grRequest.number + ' cannot be closed as not all RITMs are in the required states.');
            }
        }

       gs.info('Total processed requests: ' + totalRequestsProcessed);
        gs.info('Total requests that would have been closed: ' + totalRequestsClosed);
    } catch (ex) {
        gs.error('Error while processing requests: ' + ex.message);
    }
}



@GBS 

I believe I have provided enough guidance on this thread by sharing my script above and responding to your questions.

both count are same means your logic is correct in 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