sum of outages duration calculation for an incident to be populate dotation type custom field on INC

Shantharao
Kilo Sage

Hi All,

I need to calculate the sum of outages duration calculation for an incident to be populate dotation type custom field on Incident form

 

for example INC0001234

having 3 outages like

OUT000123 - outage duration 1 day 1 hour 10 mins 

OUT000124 - outage duration 1 day 2 hour 10 mins 

OUT000125 - outage duration 1 day 3 hour 10 mins 

total duration is 3 days 6 hours 30 mins to be populate on Incident custom duration field
Thanks

1 ACCEPTED SOLUTION

@Shantharao 

cmdb_ci_outage table doesn't have task_number field

Did you create it as reference to incident? if yes then in which scope?

I assume task_number is reference to incident table

Try this

(function executeRule(current, previous /*null when async*/) {

    // Get the related incident sys_id
    var incidentId = current.task_number; // Assuming 'task_number' is a reference to Incident's sys_id

    // Query all outages linked to this incident
    var outageGR = new GlideRecord('cmdb_ci_outage');
    outageGR.addQuery('task_number', incidentId);
    outageGR.query();

    var totalMs = 0;
    while (outageGR.next()) {
        // Assuming 'duration' is a GlideDuration field
        var durationVal = outageGR.duration.dateNumericValue(); // returns duration in ms as string
        if (durationVal) {
            totalMs += parseInt(durationVal, 10);
        }
    }

    // Update the Incident if at least one outage was found
    if (totalMs > 0) {
        var incidentGR = new GlideRecord('incident');
        if (incidentGR.get(incidentId)) {
            incidentGR.u_outage_duration.setDateNumericValue(totalMs); // Set your custom duration field
            incidentGR.update();
        }
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Shantharao 

how are you linking Outages with Incident? Via Configuration item?

You can use after update/insert business rule on "cmdb_ci_outage"

Condition: Configuration Item [IS NOT EMPTY]

Script:

1) get the current CI

2) query the outage table to know how many total outages for this CI

3) iterate and add duration

4) then query incident table with this CI and update the duration

please share some screenshots etc

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  Thank you for your response,
I have already created after update BR in the cmdb_ci_outage table but the duaration sum was not properly calculating for multiple outage scenarios,

Please find the below script and correct me with proper code

(function executeRule(current, previous /*null when async*/ ) {

    var totalDuration = new GlideDuration();
    // Get the related incident record
    var incidentGR = new GlideRecord('incident');
    // Assuming your outage table has 'incident' reference field
    if (incidentGR.get(current.task_number)) {

        //var totalDuration = 0;
        // Query all outages linked to this incident
        var outageGR = new GlideRecord('cmdb_ci_outage');
        outageGR.addQuery('task_number', current.task_number);
        outageGR.query();

        while (outageGR.next()) {
            if (outageGR.duration) { // Replace 'duration' with your outage duration field
                totalDuration +=  new GlideDuration(outageGR.duration.getDurationValue());
                gs.addInfoMessage("count=>" + outageGR.getRowCount() + " Outage num=>" + outageGR.number + " Duration=>" + outageGR.duration.getDurationValue() + " | " + outageGR.getValue("duration") + "|" + outageGR.duration.GlideDuration());
            }
        }
        gs.addInfoMessage("totalDuration=>" + totalDuration + " " + new GlideDuration(totalDuration));
        // Update the total duration field on Incident
        incidentGR.u_outage_duration = totalDuration;
        incidentGR.short_description = totalDuration;
        incidentGR.update();
    }
})(current, previous);
Thanks

@Shantharao 

cmdb_ci_outage table doesn't have task_number field

Did you create it as reference to incident? if yes then in which scope?

I assume task_number is reference to incident table

Try this

(function executeRule(current, previous /*null when async*/) {

    // Get the related incident sys_id
    var incidentId = current.task_number; // Assuming 'task_number' is a reference to Incident's sys_id

    // Query all outages linked to this incident
    var outageGR = new GlideRecord('cmdb_ci_outage');
    outageGR.addQuery('task_number', incidentId);
    outageGR.query();

    var totalMs = 0;
    while (outageGR.next()) {
        // Assuming 'duration' is a GlideDuration field
        var durationVal = outageGR.duration.dateNumericValue(); // returns duration in ms as string
        if (durationVal) {
            totalMs += parseInt(durationVal, 10);
        }
    }

    // Update the Incident if at least one outage was found
    if (totalMs > 0) {
        var incidentGR = new GlideRecord('incident');
        if (incidentGR.get(incidentId)) {
            incidentGR.u_outage_duration.setDateNumericValue(totalMs); // Set your custom duration field
            incidentGR.update();
        }
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Shantharao 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader