Schedule.duration() always returns 1970-01-01
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hello everyone,
So I have an issue with a business rule I'm writing.
I need to calculate the difference in days between three date/time fields and set the values in other fields.
The problem is that I have a schedule to follow, but using the schedule.duration() function always returns 1970-01-01, and using getNumericValue() on it just returns 0, no matter what values are in the fields.
Here is my code:
I hope someone is able to assist me,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
55m ago
your schedule is not getting loaded
try this
(function executeRule(current, previous) {
if (!current.u_inizio_attivia || !current.u_in_corso)
return;
var inizio = new GlideDateTime(current.u_inizio_attivia);
var inCorso = new GlideDateTime(current.u_in_corso);
var schedule = new GlideSchedule();
schedule.load('your_schedule_sys_id');
var durFase1 = schedule.duration(inizio, inCorso);
current.u_differenza_fase_1_fase_2 = durFase1.getRoundedDayPart();
if (current.u_attivita_conclusa) {
var fine = new GlideDateTime(current.u_attivita_conclusa);
var durFase2 = schedule.duration(inCorso, fine);
current.u_differenza_fase_2_fase_3 = durFase2.getRoundedDayPart();
}
})(current, previous);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
27m ago
Hi,
I tried this but it's still returning the same date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
22m ago
what debugging did you do?
did you add correct schedule sysid?
did you give correct field name?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
18m ago
I tried printing what is returned by schedule.duration both before and after doing
The dates inserted into the records are all good, too