- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 01:37 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 06:27 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 01:54 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 06:21 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 06:27 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 10:12 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader