
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2016 07:31 AM
Has anyone ever performed a metric duration calculation via a business rule? I have the below metric business rule that monitors reopened incident records. I'd like to calculated the duration between the mi.start and mi.end and populate the generated metric instance record's duration. I'm really asking what the correct syntax is for the code to achieve the duration calculation.
(function executeRule(current, previous /*null when async*/) {
// Create a Metric Instance
var mi = new GlideRecord('metric_instance');
mi.initialize();
mi.definition.setDisplayValue('Incident Reopened');
mi.table = 'incident';
mi.field = 'state';
mi.id = current.sys_id;
mi.calculation_complete = true;
mi.value = gs.getUserDisplayName();
mi.u_var1 = current.resolved_by.getDisplayValue();
mi.u_var2 = current.assignment_group.getDisplayValue();
mi.start = previous.resolved_at;
mi.end = gs.nowDateTime();
mi.insert();
})(current, previous);
I tried following the OOB gs command using datediff but that only caused the rule to fail and not function at all. Any ideas?
Solved! Go to Solution.
- Labels:
-
Performance Analytics
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 11:38 AM
Hey Jason,
one small error in that line, gs.dateDiff() should be write like this.
now try 17th line,
mi.duration = gs.dateDiff(mi.start, mi.end, true/false); // this was the correct syntax.
true - duration returns in seconds
false - duration returns in duration.
just one more update on duration filed was, if the field type is "string" make Boolean value true or otherwise it was type "duration" make Boolean value false.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2016 08:14 AM
Hi Jason,
You can achieve this by using GlideDateTime, please go through the below link, it will help you.
http://wiki.servicenow.com/?title=GlideDateTime#gsc.tab=0
Thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 10:02 AM
Harish,
Can you restate your response in terms of the code I posted? I viewed the article and did not find it very helpful. When I try to declare the mi.duration and use subtract from the article you reference; the business rule errors out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 10:19 AM
just add the line,
Difference = gs.dateDiff (mi.start , mi.end , true/false); // it will calculates all the days.
true - it returns in seconds
false - it returns in duration.
if you need to calculate duration based on schedule, go through with this link
http://wiki.servicenow.com/index.php?title=Calculate_Duration_Given_a_Schedule#gsc.tab=0

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 10:56 AM
Thank you for the input Balaji, unfortunately when I added the line, there was no duration calculated. The business rule did not error out and created a metric instance; however the duration still shows 0. here is the code as i have it based on your feedback:
(function executeRule(current, previous /*null when async*/) {
// Create a Metric Instance
var mi = new GlideRecord('metric_instance');
mi.initialize();
mi.definition.setDisplayValue('Incident Reopened');
mi.table = 'incident';
mi.field = 'state';
mi.id = current.sys_id;
mi.calculation_complete = true;
mi.value = gs.getUserDisplayName();
mi.u_var1 = current.resolved_by.getDisplayValue();
mi.u_var2 = current.assignment_group.getDisplayValue();
mi.start = current.resolved_at;
mi.end = gs.nowDateTime();
Difference = gs.datediff(mi.start, mi.end, false); \\here is the line i added from Balaji
mi.insert();
})(current, previous);