We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Schedule.duration() always returns 1970-01-01

SSantacroce
Tera Expert

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:

 

(function executeRule(current, previous /*null when async*/) {

    var inizio = new GlideDateTime(current.u_inizio_attivia);
    var inCorso = new GlideDateTime(current.u_in_corso);
    var fine = new GlideDateTime(current.u_attivita_conclusa);
    var schedule = new GlideSchedule('sys id of my schedule');

    var objFase1 = schedule.duration(inizio, inCorso).getRoundedDayPart();
    var durataFase1 = (objFase1.getNumericValue())/(24*60*60*1000);
    current.u_differenza_fase_1_fase_2 = durataFase1;

    if(current.u_attivita_conclusa!=''){

        var objFase2 = schedule.duration(inCorso, fine).getRoundedDayPart();
        var durataFase2 = (objFase2.getNumericValue())/(24*60*60*1000);
        current.u_differenza_fase_2_fase_3 = durataFase2;

    }  

})(current, previous);

I hope someone is able to assist me,
Thanks!
4 REPLIES 4

Ankur Bawiskar
Tera Patron

@SSantacroce 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

I tried this but it's still returning the same date

@SSantacroce 

what debugging did you do?

did you add correct schedule sysid?

did you give correct field name?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

I tried printing what is returned by schedule.duration both before and after doing 

.getRoundedDayPart(); - both times it returned 1970-01-01 00:00:00. I've verified the sysid and field names and it's all correct. The schedule works in another script I've worked on, so I don't know what the problem is. 
The dates inserted into the records are all good, too