Subtract two duration fields in business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2020 07:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2020 09:07 AM
Hi,
please check below links:
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 07:32 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2022 04:32 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader