Fix script to Cancel the RITMs

Rakesh40
Tera Contributor

Hello Everyone,

 

Can you help me with the fix script to cancel the RITMs which are older than 1 year with below condition.

Condition : if any of the Open RITMs with Opened or Closed Approvals (sysapproval_approver) and no Catalog Task.

 

Thanks

4 REPLIES 4

Sandeep Rajput
Tera Patron

@Rakesh40 See if the following works for you.

 

 

// Get the date one year ago from today
var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-1);

// Query for RITMs (sc_req_item) older than one year
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('sys_created_on', '<=', oneYearAgo);
ritmGR.addQuery('active', true); // Only consider open RITMs
ritmGR.query();

while (ritmGR.next()) {
    // Check for associated Catalog Tasks
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', ritmGR.sys_id);
    taskGR.query();
    
    if (!taskGR.hasNext()) {
        // Check for Approvals (sysapproval_approver) in Opened or Closed state
        var approvalGR = new GlideRecord('sysapproval_approver');
        approvalGR.addQuery('sysapproval', ritmGR.sys_id);
        approvalGR.addQuery('state', 'IN', 'requested,approved,rejected');
        approvalGR.query();

        if (approvalGR.hasNext()) {
            // Cancel the RITM
            ritmGR.setValue('state', 'closed_cancelled');
            ritmGR.setValue('close_code', 'Cancelled');
            ritmGR.setValue('close_notes', 'RITM cancelled due to inactivity and no associated catalog tasks.');
            ritmGR.update();
            
            gs.info('RITM ' + ritmGR.number + ' has been cancelled.');
        }
    }
}

Caution: Please test this script in your dev/sandbox environment first and if the result looks good then run on production.

 

Hi @Rakesh40 

The below screenshot will help I ran the code and its working as expected :-

RaviGaurav_0-1724765152853.png

--------Script----------

// Get the date one year ago from today
var oneYearAgo = new GlideDateTime();
oneYearAgo.addYears(-1);

// Query for RITMs (sc_req_item) older than one year
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('sys_created_on', '<=', oneYearAgo);
ritmGR.addQuery('active', true);
ritmGR.addQuery('request.state', '=', 'open');
ritmGR.query();

while (ritmGR.next()) {
// Check for associated Catalog Tasks
var hasCatalogTask = new GlideRecord('sc_task');
hasCatalogTask.addQuery('request_item', ritmGR.sys_id);
hasCatalogTask.query();
if (!hasCatalogTask.hasNext()) {
// Check for Approvals (sysapproval_approver) in Opened or Closed state
var hasApproval = new GlideRecord('sysapproval_approver');
hasApproval.addQuery('document_id', ritmGR.sys_id);
hasApproval.addQuery('state', 'IN', 'requested,approved,rejected');
hasApproval.query();
if (hasApproval.hasNext()) {
// Cancel the RITM
ritmGR.setValue('request.state', 'cancelled');
ritmGR.setValue('close_code', 'Cancelled');
ritmGR.setValue('close_notes', 'RITM cancelled due to inactivity and no associated catalog tasks.');
ritmGR.update();
gs.info('RITM ' + ritmGR.number + ' has been cancelled.');
}
}
}

 

Hello @Ravi Gaurav 

I want to Cancel the Request with RITM How can i include this condition here ?

and also If i do have Catalog Task Present for RITMs i want to set the Catalog task, RITM and REQ to Closed Automatically.

 

Hello @Sandeep Rajput 

I want to Cancel the Request How can i include this condition here ?

and also If i do have Catalog Task Present for RITMs i want to set the Catalog task, RITM and REQ to Closed Automatically.