The CreatorCon Call for Content is officially open! Get started here.

Fix Script to Close, Cancel the RITM

Rakesh40
Tera Contributor

Hello Everyone,

Can you help me with the fix script to Cancelled or Close Automatically based the RITMs which are older than 1 year with below conditions

 

Condition :

if any of the Open RITMs with Opened or Closed Approvals (sysapproval_approver) and no Catalog Task - "RITM REQ to Canceled" 

If any of the Open RITMs with Catalog Tasks - "Set the Catalog Task RITM REQ to Closed Automatically".

 

Thanks

1 REPLY 1

Sid_Takali
Kilo Patron

Hi @Rakesh40 Try below code

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

    // Query RITMs older than 1 year
    var ritmGr = new GlideRecord('sc_req_item');
    ritmGr.addQuery('opened_at', '<=', oneYearAgo);
    ritmGr.addQuery('state', '!=', '3'); // Ensure the RITMs are still open
    ritmGr.query();

    while (ritmGr.next()) {
        var ritmSysId = ritmGr.getUniqueValue();
        var hasCatalogTask = false;
        
        // Check if there are catalog tasks with the name "RITM REQ to Closed Automatically"
        var catalogTaskGr = new GlideRecord('sc_task');
        catalogTaskGr.addQuery('request_item', ritmSysId);
        catalogTaskGr.addQuery('short_description', 'Set the Catalog Task RITM REQ to Closed Automatically');
        catalogTaskGr.query();

        if (catalogTaskGr.hasNext()) {
            hasCatalogTask = true;
        }

        // Check if there are any open or closed approvals
        var approvalGr = new GlideRecord('sysapproval_approver');
        approvalGr.addQuery('sysapproval', ritmSysId);
        approvalGr.addQuery('state', '!=', '3'); // Check for any approvals that are not closed
        approvalGr.query();

        if (approvalGr.hasNext()) {
            // RITM has approvals but no catalog task to cancel it
            if (!hasCatalogTask) {
                // Cancel the RITM
                ritmGr.state = '6'; // Assuming '6' is the state for 'Cancelled'
                ritmGr.update();
                gs.info('Cancelled RITM: ' + ritmSysId);
            }
        } else if (hasCatalogTask) {
            // Close the catalog tasks
            catalogTaskGr.query();
            while (catalogTaskGr.next()) {
                catalogTaskGr.state = '3'; // Assuming '3' is the state for 'Closed'
                catalogTaskGr.update();
                gs.info('Closed Catalog Task: ' + catalogTaskGr.getUniqueValue());
            }
        }
    }