Outage based alert suppress

Chimenem
Tera Expert

 

 

I'm currently working on a task that involves some complex scripting in ServiceNow, and I could really use some guidance. Specifically, I'm facing an issue with alert suppression related to Change requests. Here's a brief overview:

 

When a Change request is created, there is an OOB Maintenance Rule (CI in change window) which sets the CI in maintenance, and the CI shows up in the em_impact_maint_ci table. Any alerts from that CI will have the "Maintenance" flag set to true.

 

Following the idea behind the 'CI in change window' Maintenance Rule, I want to ensure alert suppression occurs ONLY if the Change has an outage record created and then only suppress alerts for CIs listed on the outage record.

 

Below is the script I've created and tested but not getting expected results.

 

(function new_findCisInMaint() {
    var now = gs.nowDateTime();

 

    // 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 = newGlideRecord('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 = newGlideRecord('cmdb_ci_outage');
                outageRecord.addQuery('change_request', changeRequest.sys_id);
                outageRecord.query();
                while (outageRecord.next()) {
                    var outageCI = newGlideRecord('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();
                        }
                    }
                }
            }
        }
    }
})();

 

 

1 REPLY 1

Abbas_5
Tera Sage
Tera Sage

Hello @Chimenem,

 

Please refer to the link below:
https://www.servicenow.com/community/developer-forum/suppress-alerts-for-approved-change-requests-on...

 

If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in future it will be helpful to them.

 

Thanks & Regards,

Abbas Shaik