Fix Script to close Parent Request

Shree Nag
Tera Expert

Hello,

Appreciate all your response and help.

We have about 2K Requests which are NOT in  "closed complete" state, while their child Requested Items(RITMs) are in closed complete state. The parents requests sit in Approved state.

The fix script below is timing out when ever I run it.

Can someone please help with the Fix script to close the Parent Request items, where its requested items are in closed complete state.

 

 

var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-3);

// Query the RITM records that are active and not updated for over a year
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery('active', true);
ritmGr.addQuery('sys_updated_on', '<', oneYearAgo);
ritmGr.query();

while (ritmGr.next()) {
    // Close the RITM record
    ritmGr.state = 'closed_complete'; // or the appropriate closed state value
    ritmGr.update();
    
    // Get associated REQUEST record and close it
    var requestGr = new GlideRecord('sc_request');
    if (requestGr.get(ritmGr.request)) {
        requestGr.state = 'closed_complete'; // or the appropriate closed state value
        requestGr.update();
    }
    
    // Close associated SCTASK records
    var sctaskGr = new GlideRecord('sc_task');
    sctaskGr.addQuery('request_item', ritmGr.sys_id);
    sctaskGr.addQuery('state', '!=', 'closed_complete'); // Only update tasks that are not already closed
    sctaskGr.query();
    
    while (sctaskGr.next()) {
        sctaskGr.state = 'closed_complete'; // or the appropriate closed state value
        sctaskGr.update();
    }
}

gs.info('RITMs, Requests, and SCTasks have been closed successfully.');

 

 

7 REPLIES 7

Thank you for the response @Its_Azar .

Make sense to get only the requests not in Closed complete state.

 

I executed teh script, I got a message , with ) requests Closed:

 

Script: 

Completed: Push in 0:00:00.014, next occurrence is 2025-08-07 18:08:46, id=4ed15122ff2331009738ffffffffff06 tr=10 pr=25 re=10 po=1754590116 la=0.433
Time: 0:00:00.233 id: Myinstance[glide.19] primary_hash=-857152021 (connpid=1754955) for: SELECT task0.`sys_id` FROM task task0 WHERE task0.`sys_class_name` IN ('sc_request','kb_knowledge_base_request') AND task0.`state` != NULL /* Myinstance, gs:C474725C1B1B22104758C883604BCBBC, tx:c5367adc1b1b22104758c883604bcb69, hash:-857152021 */
*** Script: Total parent requests closed: 0
[0:00:00.257] Total Time

 

great if this helps do accept the solution

ā˜‘ļø If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

ļ† Microsoft MVP (AI Services), India

Aniket1498
Giga Guru

Hi @Shree Nag 

To update data it is always best practice to use the 'Data Management Jobs->Update Jobs' if the query can be generated. For your case, it seems that the tickets can be closed using 2-3 update jobs. Update jobs is a low-code approach and in case of any issues, it can be rolled back.

 

Step 1: Get the 'active=true^sys_updated_on<3years' for RITMs.

Step 2: Create a update job on sc_req_item and set the Status value to 'Closed Complete'

Step 3: Create another update job on sc_task and put the condition as 'Request Item is one of the' and add the requested item numbers you fetched from Step 1. Finally set the status value to 'Close Complete'

Step 4: Repeat similar as Step 3 fo sc_request and you can get the request details from Step 1.

 

Please accept the solution or mark this as helpful if your issue is resolved with the approach.

 

Best Regards,

Aniket Dey.