- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 03:01 AM
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/
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 12:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 03:35 AM - edited 10-11-2022 03:36 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 11:50 PM
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 12:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2022 12:03 AM - edited 10-17-2022 12:07 AM
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.
On the cmdb_ci_outage form, the "task_number" reference field hold the reference to the incident in scope.
Lastly, i guess that a business rule should hold the script? So on insert/update the script should run?
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/