Calculate duration on incident based on Outage related list

AndersBGS
Tera Patron
Tera Patron

Hi all, 

 

Can anybody please help on how to calculate duration between field on the incident form and field on the outage form? Incident.created subtracted from CMDB_CI_Outage.begin. 

 

The outage record is listed in the outage related list on the incident form.

I guess i should utilize a function like "function getTimeDiff(){ "

 

Please let me have your suggestion how to do this best possible. 

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

1 ACCEPTED SOLUTION

Yes, then we just turn the script around, so we look up the incident instead of the outage! See updated script below

 

var outStart = current.sys_created_on.getDisplayValue(); // Outage begin
var incId = current.getValue('task_number'); //we get the incident
var incBegin = '';

//Here we query the incident that is related to the incident above. 
var incGr = new GlideRecord('incident');
incGr.addQuery('sys_id', incId);
incGr.query();
if(incGr.next()) {
incBegin = incGr.begin.getDisplayValue();
}
if(incBegin) { //Let's be sure that we found an incident
//We save the duration from incident begin (incBegin) to outage start (outStart) in the value duration.. 
var duration = gs.calDateDiff(incBegin, outStart , false); 
current.u_outage_incident_created = duration; 
}

 

Since the outage is linked to a task, then I would only have the  business rule on insert. Also make sure that it has a "task_number is not empty" in the condition! 🙂 


Best regards,
Sebastian Laursen

View solution in original post

7 REPLIES 7

Sebastian L
Mega Sage

Hi Anders,

Yes, you should be able to do it with calDateDiff(): 

In my example below the calculation is done on the same object (emp) using opened_at and closed_at, but you of course just need to do the calculation based on your two objects and the two different dates. 

 

        var opened = emp.opened_at.getDisplayValue();
        var resolved = emp.closed_at.getDisplayValue();

        emp.business_duration = gs.calDateDiff(opened, resolved, false);

 

 

Hope it makes sense. 


Best regards,
Sebastian Laursen

Hi Sebastian, 

 

I understand what you wrote, but the thing that I do not understand, is how to glide to related records 🙂

How do I point at the related record for one of the variables? e.g.:

 

var outage_start = cmdb_ci_outage.begin.getDisplayValue();

var incident_created = incident.created.getDisplayValue();

 

Time from outage begin to incident created = gs.calDateDiff(outage_start, incident_created, false);

 

Appreciate your help

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Hi Anders,

 

So not sure where you want to save the value, but if we assume it is on the incident record then it can look something like this. I also assume that the incident record is stored in the task_number field on the outage (so that is where you have the relationship between them): 

 

var incStart = current.sys_created_on.getDisplayValue(); //I assume we are on the incident record so we can use current.field to get a value 
var incId = current.getValue('sys_id'); //We store the sys_id of the incident so we can look for the outage that is linked to it
var outageBegin = '';

//Here we query the outage that is related to the incident above. 
var outageGr = new GlideRecord('cmdb_ci_outage');
outageGr.addQuery('task_number', incId);
outageGr.query();
if(outageGr.next()) {
outageBegin = outageGr.begin.getDisplayValue();
}
//We save the duration from outagebegin to incident start in the value duration.. Don't know where you want to save this?
var duration = gs.calDateDiff(outageBegin, incStart, false); 

 

Else I might need to know how you have setup your relation between incidents and outages, and also where you want to save the value. In the above I asume it is mostly out of the box!


Best regards,
Sebastian Laursen

Hi Sebastian, 

 

Thank you so much for your help. 

 

My idea was actually to have a duration field on the cmdb_ci_outage record called "u_outage_incident_created" where the calculated duration should be placed.

 

AndersSkovbjer_0-1665990338136.png

 

On the cmdb_ci_outage form, the "task_number" reference field hold the reference to the incident in scope. 

AndersSkovbjer_1-1665990380813.png

 

Lastly, i guess that a business rule should hold the script? So on insert/update the script should run?

AndersSkovbjer_2-1665990429053.png

 

Please let me know if any additional input is missing.

 

Again, really appreciate your help on this topic.

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/