- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2020 08:27 PM
Hello All,
I am working in scoped application and have to convert duration field into days not in hours or minute. it is giving the duration in like. I wants to convert minutes or hours in Days only. I have tried using getDay or getDayPart/getNumeric value function but they gives the result. Any suggestion.
I have written a business rule and code is :-
(function executeRule(current, previous /*null when async*/) {
var opened = new GlideDateTime(current.opened_at.getDisplayValue());
gs.info("open time is " +current.opened_at.getDisplayValue());
var closed = new GlideDateTime(current.closed_at.getDisplayValue());
gs.info("close time is " +current.closed_at.getDisplayValue());
var duration = GlideDateTime.subtract(opened, closed);
gs.info("Duration is " + duration);
current.calendar_duration = duration;
current.update();
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Scoped App Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2020 11:09 PM
If it is duration then it would show in that format only.
Even with max_unit attribute it would show in partial format.
Not possible to hold only days value.
If that is required for your reporting purpose I would suggest to create new field and store the days into that
Note: creating field will have cost implication as per your servicenow subscription
To get only days from the duration
Avoid current.update() in business rules
(function executeRule(current, previous /*null when async*/) {
var opened = new GlideDateTime(current.opened_at.getDisplayValue());
gs.info("open time is " +current.opened_at.getDisplayValue());
var closed = new GlideDateTime(current.closed_at.getDisplayValue());
gs.info("close time is " +current.closed_at.getDisplayValue());
var duration = GlideDateTime.subtract(opened, closed);
gs.info("Duration is " + duration);
var days = (duration.dateNumericValue())/86400000; // this can be integer or float as well
current.<new_field> = days;
current.update();
})(current, previous);
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
‎09-29-2020 10:09 PM
It will always convert showing 24h as 1 day. So 1 day is a unit. Like 60sec will show as 1 minute.
However you can use the max_unit attribute to for example show it in hours exactly.
The max_unit dictionary attribute defines the maximum unit of time used in a duration. For example, if max_unit=minutes
, a duration of 3 hours 5 minutes 15 seconds appears as 185 minutes 15 seconds. To set the maximum unit of duration measurement, add the following dictionary attribute to the duration field:
max_unit=<unit>
https://docs.servicenow.com/bundle/madrid-application-development/page/script/general-scripting/conc...
The other way around is not possible. To have a Duration type field only show complete days. You have to create a new field type string for example and set the number of days in that, in the format you like.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2020 10:48 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2020 11:09 PM
If it is duration then it would show in that format only.
Even with max_unit attribute it would show in partial format.
Not possible to hold only days value.
If that is required for your reporting purpose I would suggest to create new field and store the days into that
Note: creating field will have cost implication as per your servicenow subscription
To get only days from the duration
Avoid current.update() in business rules
(function executeRule(current, previous /*null when async*/) {
var opened = new GlideDateTime(current.opened_at.getDisplayValue());
gs.info("open time is " +current.opened_at.getDisplayValue());
var closed = new GlideDateTime(current.closed_at.getDisplayValue());
gs.info("close time is " +current.closed_at.getDisplayValue());
var duration = GlideDateTime.subtract(opened, closed);
gs.info("Duration is " + duration);
var days = (duration.dateNumericValue())/86400000; // this can be integer or float as well
current.<new_field> = days;
current.update();
})(current, previous);
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
‎10-05-2020 03:09 AM
Hope you are doing good.
Let me know if I have answered your question.
If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.
If not, please let us know if you need some more assistance.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader