Showing a Duration field as a number of Days only (Aged Tickets)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2017 01:37 AM
Firstly I'm not a developer, so please be patient. I have created a duration field (Integer) u_ticket_age on the Task table and a calculated script compiled for a number of sources on this site in the dictionary entry for u_ticket_age.
the code I have used is:-
Istanbul version
// variables available
// current: GlideRecord - target task
// definition: GlideRecord - (this row)
var s = current.state;
var ticket = current.task.sys_class_name;
if (ticket == Incident && s >=6){
gs.dateDiff(current.sys_created_on.getDisplayValue(),current.sys_updated_on.getDisplayValue(),false);
} else if (ticket !== Incident && s >=3){
gs.dateDiff(current.sys_created_on.getDisplayValue(),current.sys_updated_on.getDisplayValue(),false);
} else {
gs.dateDiff(current.sys_created_on.getDisplayValue(),gs.nowDateTime(),false);
}
This generates the following format E.G. 23 Days 7 Hours 42 Minutes
However, I would like it to show Days only. 23 Days or rounding up/down depending on Hours and minutes.
I have found the attribute max_unit=hours / minutes / seconds but there doesn't appear to be one for Days
I have used the ByFormat('ddd') but this shows incorrect Days for most tickets (mainly Open, but is inconsistent). See example from the report screen shot below "Age" is a string field so when we export to excel it will export "23 Days" and not as an integer number format.
Here I use current.u_age = current.u_ticket_age.getByFormat("ddd"); in the u_age calculated script field, but I have included the getByFormat (again sourced from this site) resulting in the following. Once I have the format in Days, I will remove ticket age from the report.
Any ideas? Any help would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-20-2017 11:43 AM
Hi Terence,
This post may help:
https://community.servicenow.com/thread/174844
Thanks
Shruti
If the reply was informational, please like, mark as helpful or mark as correct!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2017 01:05 AM
Hi Shruti,
Unfortunately this didn't work.
If I set the gs.dateDiff to True (see below, without any calculations) I still get the report showing 23 Days, 7 Hours, 23 minutes etc...
the only way I get the gs.dateDiff to display the time in seconds is to set the field attribute to max_unit=seconds. But as the attribute is set, I can't use the calculation, as the attribute over writes the calculation and shows the value in seconds.
u_ticket_age (integer) to show age of ticket depending on ticket type and state
var s = current.state;
var ticket = current.task.sys_class_name;
if (ticket == Incident && s >=6){
gs.dateDiff(current.sys_created_on.getDisplayValue(),current.sys_updated_on.getDisplayValue(), true);
} else if (ticket !== Incident && s >=3){
gs.dateDiff(current.sys_created_on.getDisplayValue(),current.sys_updated_on.getDisplayValue(), true);
} else {
gs.dateDiff(current.sys_created_on.getDisplayValue(),gs.nowDateTime(), true);
}
So if I then move to u_age (string) so the information can be exported into excel showing the data and not converted to an integer, and perform the calculation here I just end up with "NaN" in the field. Which seems to indicate I'm trying to perform a calculation on the string field and not an integer field. How do I set answer to an integer field?
var answer = current.u_ticket_age; // copy u_ticket_age (Integer field) to answer
answer = answer / 86400; // 86400 SECONDS in a day, this converts to days as a decimal.
answer = Math.round(answer); // Math.round rounds to the nearest whole day
current.u_age = answer.getDisplayValue();// copies answer to u_age
Or can you suggest anything else?
Thanks
Terry