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

harishdasari
Tera Guru

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.


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.


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


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