Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to calculate business duration in metric definition

Anand Kothapall
Kilo Explorer

HI All,

 

      Requirement is to calculate business duration when HR case is moved from work-in progress to closed complete, I am getting correct duration but not business duration. I am getting business duration is 0.

Below is the script.

var state = current.state;
if (state == '4' || state == '10')
timeDurations(state);function timeDurations(value) {
var mi = new MetricInstance(definition, current);
if (!mi.metricExists() && value == '4') {
var gr1 = mi.getNewRecord();
gr1.start = current.sys_updated_on;
gr1.calculation_complete = false;
gr1.insert();
return;
}
if (mi.metricExists() && value == '4') {
var gr2 = new GlideRecord('metric_instance');
gr2.addQuery('id', current.sys_id);
gr2.addQuery('definition', 'af5f21a71b837010212c5467624bcba6');
gr2.orderByDesc('sys_created_on');
gr2.query();
if (gr2.next()) {
var gdt = new GlideDateTime(current.sys_updated_on.getDisplayValue());
var ms = gr2.duration.dateNumericValue();gdt.add(-ms);
gr2.start = gdt.getValue();
gr2.end = '';
gr2.duration = '';
gr2.calculation_complete = false;
gr2.update();
}
return;
}
if (mi.metricExists() && value == '10') {
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('definition', 'af5f21a71b837010212c5467624bcba6');
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
gr.end = current.sys_updated_on;
var gsBusiness =new GlideSchedule('bfbf1b8d1b533010212c5467624bcbba');
gr.business_duration = gsBusiness.duration(gr.start.getGlideObject(), gr.end.getGlideObject());
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.update();
}
return;
}
}

If any one know please suggest, it's bit quite urgent.

Thanks in advance.

3 REPLIES 3

Community Alums
Not applicable

HI,

The Business duration would be :

gr.business_duration = gs.calDateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());

Please mark my answer as Correct & Helpful, if applicable.

Thanks

Sandeep

Hi Sandeep,

 

      Tried with above also but still getting business duration is empty.

find_real_file.png

Gaurav Banga
Kilo Guru

Hi Anand,

 

Please refer below blog on how to get the business duration:

https://community.servicenow.com/community?id=community_question&sys_id=dccdce52db819f04c310fb651f96...

Thanks,

Gaurav