Create Location-Based SLA PA Indicators (Incident/Request)

Patricio Javier
Tera Contributor
//🅟🅙🅜🅐 🅓🅔🅥🅔🅛🅞🅟🅔🅡
(function() {
    // Location Configuration
    var locations = [{
        name: "Chile",
        location_sys_id: "???",
        filter_field: "location",
        filter_operator: "="
    }];

    // System Reference IDs
    var unit_percent_sys_id = "???";
    var unit_number_sys_id = "???";
    var frequency_quarterly = "60";

    // Function to create automated indicator
    function createAutomatedIndicator(name, conditions, table, direction, description) {
        var gr = new GlideRecord("pa_indicators");
        gr.initialize();
        gr.setValue("name", name);
        gr.setValue("sys_name", name);
        gr.setValue("conditions", conditions);
        gr.setValue("table", table);
        gr.setValue("type", "1"); // Automated
        gr.setValue("frequency", frequency_quarterly);
        gr.setValue("unit", unit_number_sys_id);
        gr.setValue("direction", direction);
        gr.setValue("collect_records", true);
        gr.setValue("precision", "0");
        gr.setValue("description", description);
        
        var sysId = gr.insert();
        if (sysId) {
            gs.info("Created indicator: " + name + " - " + sysId);
        } else {
            gs.error("Error creating indicator: " + name);
        }
        return sysId;
    }

    // Function to create formula indicator
    function createFormulaIndicator(name, formula, direction, description) {
        var gr = new GlideRecord("pa_indicators");
        gr.initialize();
        gr.setValue("name", name);
        gr.setValue("sys_name", name);
        gr.setValue("type", "2"); // Formula
        gr.setValue("formula", formula);
        gr.setValue("formula_allow_null", false);
        gr.setValue("frequency", "10"); // Daily
        gr.setValue("unit", unit_percent_sys_id);
        gr.setValue("direction", direction);
        gr.setValue("precision", "2");
        gr.setValue("description", description);
        
        var sysId = gr.insert();
        if (sysId) {
            gs.info("Created formula indicator: " + name + " - " + sysId);
        } else {
            gs.error("Error creating formula indicator: " + name);
        }
        return sysId;
    }

    // Create SLA KPIs for each location
    locations.forEach(function(loc) {
        gs.info("=== Creating SLA KPIs for: " + loc.name + " ===");
        
        try {
            // Note: The original code uses 'loc.filter_value', which is not defined in the 'locations' array.
            // You might need to change 'loc.filter_value' to 'loc.location_sys_id' for it to work.
            // This translation keeps the original 'loc.filter_value' as requested.
            var locationFilter = loc.filter_field + loc.filter_operator + loc.filter_value;

            // 1. INCIDENTS MET SLA (within target time)
            var conditionsIncidentMet = locationFilter + 
                '^made_sla=true^sys_created_onONThis quarter@javascript:gs.beginningOfThisQuarter()@javascript:gs.endOfThisQuarter()';
            
            var incidentMetSysId = createAutomatedIndicator(
                "KPI · Incidents SLA Met in Quarter in " + loc.name,
                conditionsIncidentMet,
                "incident",
                "1", // Maximize
                "Number of incidents that met SLA in the quarter for " + loc.name
            );

            // 2. TOTAL INCIDENTS WITH APPLICABLE SLA
            var conditionsIncidentTotal = locationFilter + 
                '^sla_dueISNOTEMPTY^sys_created_onONThis quarter@javascript:gs.beginningOfThisQuarter()@javascript:gs.endOfThisQuarter()';
            
            var incidentTotalSysId = createAutomatedIndicator(
                "KPI · Total Incidents with SLA in Quarter in " + loc.name,
                conditionsIncidentTotal,
                "incident",
                "3", // None
                "Total number of incidents with applicable SLA in the quarter for " + loc.name
            );

            // 3. REQUESTS MET SLA (within target time)
            var conditionsRequestMet = locationFilter + 
                '^made_sla=true^sys_created_onONThis quarter@javascript:gs.beginningOfThisQuarter()@javascript:gs.endOfThisQuarter()';
            
            var requestMetSysId = createAutomatedIndicator(
                "KPI · Requests SLA Met in Quarter in " + loc.name,
                conditionsRequestMet,
                "sc_request",
                "1", // Maximize
                "Number of requests that met SLA in the quarter for " + loc.name
            );

            // 4. TOTAL REQUESTS WITH APPLICABLE SLA
            var conditionsRequestTotal = locationFilter + 
                '^sla_dueISNOTEMPTY^sys_created_onONThis quarter@javascript:gs.beginningOfThisQuarter()@javascript:gs.endOfThisQuarter()';
            
            var requestTotalSysId = createAutomatedIndicator(
                "KPI · Total Requests with SLA in Quarter in " + loc.name,
                conditionsRequestTotal,
                "sc_request",
                "3", // None
                "Total number of requests with applicable SLA in the quarter for " + loc.name
            );

            // 5. FORMULA: % INCIDENT SLA COMPLIANCE
            if (incidentMetSysId && incidentTotalSysId) {
                var formulaIncidentSLA = "(([[" + incidentMetSysId + "]])) / (([[" + incidentTotalSysId + "]]) || 1) * 100";
                
                createFormulaIndicator(
                    "KPI · % Incident SLA Compliance in Quarter in " + loc.name,
                    formulaIncidentSLA,
                    "1", // Maximize
                    "Percentage of SLA compliance for incidents in " + loc.name + ". Formula: (Incidents that Met SLA / Total Incidents with SLA) * 100"
                );
            }

            // 6. FORMULA: % REQUEST SLA COMPLIANCE
            if (requestMetSysId && requestTotalSysId) {
                var formulaRequestSLA = "(([[" + requestMetSysId + "]])) / (([[" + requestTotalSysId + "]]) || 1) * 100";
                
                createFormulaIndicator(
                    "KPI · % Request SLA Compliance in Quarter in " + loc.name,
                    formulaRequestSLA,
                    "1", // Maximize
                    "Percentage of SLA compliance for requests in " + loc.name + ". Formula: (Requests that Met SLA / Total Requests with SLA) * 100"
                );
            }

            // 7. FORMULA: % COMBINED SLA COMPLIANCE (Incidents + Requests)
            if (incidentMetSysId && incidentTotalSysId && requestMetSysId && requestTotalSysId) {
                var formulaCombinedSLA = "(([[" + incidentMetSysId + "]] + [[" + requestMetSysId + "]])) / " +
                                         "(([[" + incidentTotalSysId + "]] + [[" + requestTotalSysId + "]])) * 100";
                
                createFormulaIndicator(
                    "KPI · % Overall SLA Compliance in Quarter in " + loc.name,
                    formulaCombinedSLA,
                    "1", // Maximize
                    "Combined SLA compliance percentage (incidents + requests) in " + loc.name + ". Formula: (Total tickets that Met SLA / Total tickets with SLA) * 100"
                );
            }

        } catch (e) {
            gs.error("Error creating SLA KPIs for " + loc.name + ": " + e.toString());
        }
    });

    gs.info("=== SLA KPI creation process completed ===");

})();
0 REPLIES 0