Fix Script to Close Automatically the State

Rakesh40
Tera Contributor

Hi Everyone,

I have written a Fix script to close the RITM records which are active and not updated before one year ago and also there associated REQ, APPROVAL and SCTASK state as Closed Automatically.

 

But i don't want to lookup for all tables. Sometimes there will be no approvals and No tasks available.
Only lookup only if Approvals and SCTASKS available and get them closed automatically.
How can i achieve this??

 

 

 

// 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_automatically; // closed_automatically
    ritmGr.setValue('active', 'false');
    ritmGr.update();
    
    // Get associated REQUEST record and close it
    var requestGr = new GlideRecord('sc_request');
    if (requestGr.get(ritmGr.request)) {
        requestGr.state = 'closed_automatically'; // closed_automatically
        requestGr.setValue('active', 'false');
        requestGr.update();
    }
    
// Close associated Approvals 
    var apprGr = new GlideRecord('sysapproval_approver');
    apprGr.addQuery('sysapproval.sys_id', requestItemGR.sys_id);
    apprGr.addQuery('state', 'requested'); 
    apprGr.query();
    
    while (apprGr.next()) {
        apprGr.state = 'cancelled'; //Cancelled
        apprGr.update();
    }
}
    // Close associated SCTASK records
    var sctaskGr = new GlideRecord('sc_task');
    sctaskGr.addQuery('request_item', ritmGr.sys_id);
    sctaskGr.addQuery('state', '!=', 'closed_complete'); 
    sctaskGr.setValue('active', 'false')
    sctaskGr.query();
        while (sctaskGr.next()) {
        sctaskGr.state = 'closed_automatically'; // closed_automatically
        sctaskGr.update();
      }
}

 

 

 Thanks

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@Rakesh40 You can add GlideAggregate COUNT queries to check if there are approvals/sc_task associated with an RITM. However, you can't completely eliminate the GlideRecord queries as the tables needs to be queried to update the records in them. 

Alp Utku
Mega Sage

You can use flow designer to satisfy your requirements without coding.

Community Alums
Not applicable

I agree flow designer would be easier solution to maintain 

Deepak Shaerma
Kilo Sage

Hi @Rakesh40 

Try using encoded query and please ensure that state field backend value is correct for all tables... you used closed_automaticaaly in all the gliderecords.. i think for closed complete it was '3'. 

Regards
Deepak Sharma