Subtract two duration fields in business rule

Ksnow
Tera Contributor

I want to subtract the duration fields based on my requirement as below:

Time should be calculated from when the assigned to filled in to assignment group changes, so I am going with BR.

This duration has to be displayed in my incident form custom field(duration field) named as testing_time

Condition : Assignment group changes

In script:

var assignedTimestart;

// metric_instance table to get the start value of assignedto duration
var gr = new GlideRecord("metric_instance");
gr.addQuery("id", current.sys_id );
gr.addQuery("definition","6f30fc96db230010bfd0f28239961954");// assigned to duration metric sys_id
gr.query();
if (gr.next()) {
assignedTimestart = new GlideDateTime(gr.getDisplayValue("start"));
}

var nowUTC = new GlideDateTime(gs.nowDateTime());

dur = GlideDateTime.subtract(assignedTimestart, nowUTC);

var dur = new GlideDuration();

gs.addInfoMessage(dur.getDisplayValue());

// this time has to be subtracted from the above time if the state is onhold or resolved

var gr1 = new GlideRecord("metric_instance");
 gr1.addQuery("id", current.sys_id );
 gr1.addEncodedQuery("value=On Hold^ORvalue=Resolved");

 gr1.query();
 while (gr1.next()) {
var value = new GlideDateTime(gr.duration);
 dur = GlideDateTime.subtract(value,dur) ; // subtract 1st value(assignedto start time - current time)-2nd                                                                                                                                        value(onhold^resolved time)
 }

current.u_testing_time = dur;

This is not working for me, value is not getting appended in the custom field(u_testing_time) of incident.

NOTE: when I use gs.dateDiff(date1.getDisplayValue(), date2.getDisplayValue(), false); displayed wrong value as 1876 days 45 hours.

I ran bg script then I get 1970-01-01 00.01.56

Please help me with the accurate solution.

Thanks,

K

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please check below links:

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

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

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ksnow 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ksnow 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader