Fix script to close the RITM and Associated REQ and SCTASK

Rakesh40
Tera Contributor

Hello All,

 

Can please someone help me with the Fix script to close the RITM records which are active and not updated before one year ago. 

i need to fetch data encoded query on RITM.

"active=true^sys_updated_on<javascript&colon;gs.beginningOfOneYearAgo()"

I need to set the RITM state as Closed Automatically and also associated REQ and SCTASK state as Closed Automatically.

 

Thanks

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Rakesh40 Please see if this script works for you.

// Define the time threshold (one year ago)
var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-1);

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

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Rakesh40 Please see if this script works for you.

// Define the time threshold (one year ago)
var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-1);

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

@Rakesh40 Please mark this an accepted solution if it managed to address your question.

Hi Sandeep, can you help in resolving the issue I have. 

Even though I close all the SCTASKS under the RITM, the RITM is not closed complete automatically. 

I somehow got this script but it is also not helping 

 

var sc = new GlideRecord('sc_task');
    sc.addQuery('request_item', current.request_item);
    sc.query();
    var sctotalcount = sc.getRowCount();
    var sc1 = new GlideRecord('sc_task');
    sc1.addQuery('state', IN, '3,4,7');
    sc1.query();
    var totalclosedsctask = sc1.getRowCount();
    if (totalCount == totalclosedsctask) {
        var ritm = current.request_item.getRefRecord();
        ritm.state = 3;
        ritm.update();
    }
})(current,previous)