- 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
10-10-2020 12:31 AM
Thanks for marking my response as helpful.
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.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader