
- 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-26-2016 11:20 AM
in which variable you are adding this variable?
what is the type of field?
paste your screen shot also.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 11:28 AM
I am trying to calculate the difference between mi.start and mi.end as a duration. I want to store the calculation in the metric_instance.duration field which is a glide_duration data type column. I am new to scripting in service now so I have a bit of a learning curve so I apologize if what I am asking is in anyway ambiguous. Here is a screenshot of my code. It is a business rule in the metrics application. What I am working to accomplish is document an incident reopen (see condition) and capture key data when a reopen occurs. The duration I want to calculate is the amount of time the record was resolved before being reopened (non-value time).
- 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-26-2016 11:58 AM
That worked! I appreciate your help and insight on this Balaji!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2016 12:05 PM
Glad that your question was completed