Calculate duration with a business rule in metrics

jasonhagen
Kilo Contributor

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?

1 ACCEPTED SOLUTION

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.


View solution in original post

9 REPLIES 9

BALAJI40
Mega Sage

in which variable you are adding this variable?


what is the type of field?


paste your screen shot also.


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).



find_real_file.png


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.


jasonhagen
Kilo Contributor

That worked!   I appreciate your help and insight on this Balaji!  


Glad that your question was completed