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

Suppress alerts only for CIs on the outage record.

SNLearnAll
Tera Contributor

I want to ensure alerts are suppressed only for CIs listed in the outage records (cmdb_outage_ci_mtom) during the planned maintenance window, it is instead suppressing alert for CIs in the Affected CIs (task_ci) tab of the Change even before an Outage record is created. Any corrections to the script or alternative script to achieve the result is much appreciated.

 

(function new_findCisInMaint() {
    var now = gs.nowDateTime(); // Get the current time in Mountain Time

    // Query to find change requests that are currently active and approved within the planned window
    var queryChanges = "start_date<=" + now + "^end_date>=" + now + "^stateIN-2,-1,0^approval=approved^work_start<=" + now + "^work_end>=" + now + "^work_startISNOTEMPTY^work_endISEMPTY^ORwork_end>=" + now;

    // Get the sys_id of change requests that match the query
    var changesInActiveWindow = getRecordsSysId('change_request', queryChanges, "sys_id");

    // Query to find outage records associated with the change requests in the active window
    var queryOutageRecords = "change_request.sys_idIN" + changesInActiveWindow.toString();

    // Get the sys_id of outage records that match the query
    var outageRecords = getRecordsSysId('cmdb_ci_outage', queryOutageRecords, "sys_id");

    // Query to find impacted CIs associated with the outage records
    var queryImpactedCis = "outage_record.sys_idIN" + outageRecords.toString();

    // Get the CI IDs from the outage records
    var cis = getRecordsSysId('cmdb_outage_ci_mtom', queryImpactedCis, "ci_item");

    // Clear maintenance flag for CIs if the change request is closed or cancelled
    clearMaintenanceFlag(changesInActiveWindow);

    // Return the CI IDs as a JSON string
    return JSON.stringify(cis);

    /**
     * Helper function to get records' sys_id based on a query
     * @Param {string} table - The table name to query
     * @Param {string} query - The encoded query string
     * @Param {string} attribute - The attribute to retrieve (e.g., sys_id)
     * @returns {Array} - Array of attribute values
     */
    function getRecordsSysId(table, query, attribute) {
        var gr = new GlideRecord(table); // Initialize GlideRecord for the specified table
        var results = []; // Array to store the results
        gr.addEncodedQuery(query); // Add the encoded query to the GlideRecord
        gr.query(); // Execute the query
        while (gr.next()) {
            results.push(gr.getValue(attribute)); // Add the attribute value to the results array
        }
        return results; // Return the results array
    }

    /**
     * Function to clear maintenance flag for CIs if the change request is closed or cancelled
     * @Param {Array} changeRequestIds - Array of change request sys_ids
     */
    function clearMaintenanceFlag(changeRequestIds) {
        var changeRequest = new GlideRecord('change_request');
        changeRequest.addQuery('sys_id', 'IN', changeRequestIds);
        changeRequest.query();
        while (changeRequest.next()) {
            if (changeRequest.state == '3' || changeRequest.state == '4') { // Check for closed or cancelled states
                var outageRecord = new GlideRecord('cmdb_ci_outage');
                outageRecord.addQuery('change_request', changeRequest.sys_id);
                outageRecord.query();
                while (outageRecord.next()) {
                    var outageCI = new GlideRecord('cmdb_outage_ci_mtom');
                    outageCI.addQuery('outage_record', outageRecord.sys_id);
                    outageCI.query();
                    while (outageCI.next()) {
                        var alert = new GlideRecord('em_alert');
                        alert.addQuery('ci_id', outageCI.ci_item);
                        alert.query();
                        while (alert.next()) {
                            alert.setValue('maintenance', false); // Clear the maintenance flag
                            alert.update();
                        }
                    }
                }
            }
        }
    }
})();

 

0 REPLIES 0