Does GlideSchedule() works?

Vasilis Anastas
Tera Expert
Hello, I am trying to run a background script with below code in order to automatically close all resolved incidents after 3days or more. I want to add that in a scheduled job, but first I am trying it through background script:
var g = new GlideRecord('incident');
g.addQuery('state', '6');
g.query();
while (g.next())
{
    var days = 3;
    var startDate = g.resolved_at;
    var endDate = gs.now();
    var schedule = new GlideSchedule();
    schedule.load('c178fd23832ed61073c5b2b6feaad311'); // loads "8-5 weekdays excluding holidays" schedule
    var duration = schedule.duration(startDate, endDate);
    //gs.info(duration.getDurationValue()); // gets the elapsed time in schedule
    if (duration >= days)
    {
        g.state = '7'; // set as closed.
    }
}

I am getting this error: 
Script execution error: Script Identifier: null.null.script, Error Description: Can't find method com.glide.schedules.Schedule.duration(java.lang.String,string). (null.null.script; line 16), Script ES Level: 0 Javascript compiler exception: Can't find method com.glide.schedules.Schedule.duration(java.lang.String,string). (null.null.script; line 16) in
 
 
any idea?
 
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Vasilis Anastas 

solution is present in this link. check that and enhance

Auto closure of Task after 3 business days 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Juhi Poddar
Kilo Patron

Hello @Vasilis Anastas 

The issue here is not with GlideSchedule() instead it is with schedule.duration.

To get the difference between two dates:

 

var duration = endDate.getNumericValue() - startDate.getNumericValue(); //return milliseconds

 

The script can be further updated as:

 

var g = new GlideRecord('incident');
g.addQuery('state', '6');
g.query();
while (g.next()) {
    var days = 3;
    var startDate = g.resolved_at;
    var endDate = gs.now();
    var schedule = new GlideSchedule();
    schedule.load('c178fd23832ed61073c5b2b6feaad311'); // loads "8-5 weekdays excluding holidays" schedule
    var duration = endDate.getNumericValue() - startDate.getNumericValue(); //return diff in milli seconds
    if (duration >= days*86400000) { //one day is 86400000ms
        g.state = '7'; // set as closed.
    }
}

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar