How to incorporate schedule in a scheduled job

Community Alums
Not applicable

Hello, actually I have got one requirement to add a field on the Risk event form (Working days open). There is also another field on the form which is the 'Date Reported' which gets autopopulated on the day which the risk event has created.

So , I need the 'Working days open' field to get autopopulated with only the working days, including the 'Date Reported' and also to exclude the weekends and the UK bank holidays. So I have created a schedule of '8-5 weekdays excluding uk holidays' and trying to incorporate that schedule in my scheduled job but unfortunately, I can't do that. Can someone please me in solving this ? Below is my code that is giving me all the days .

 

var today = new GlideDateTime();
var schedule = new GlideSchedule('sys id of my schedule');
var riskEvent = new GlideRecord('sn_risk_advanced_event');
riskEvent.addEncodedQuery('active=true');
riskEvent.query();
while (riskEvent.next()) {
    var reportedDate = new GlideDateTime(riskEvent.opened_at);
    var duration = today.getNumericValue() - reportedDate.getNumericValue();
    var days = Math.floor(duration/ (1000 * 60 * 60 * 24)); //Converting the duration to days
    riskEvent.u_working_days_open = days + 1 // Adding + 1 for including the Date reported
    riskEvent.update();

}
 
Much Appreciated of your help, Thank you!

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

so basically you want to subtract the dates using schedule

Recently I shared solution for something similar, check that

calculate Duration and Business Duration in scoped application 

also check these links

subtract time from schedule to update task due date 

Calculate Date By Adding or Subtracting Time With A Schedule 

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

so basically you want to subtract the dates using schedule

Recently I shared solution for something similar, check that

calculate Duration and Business Duration in scoped application 

also check these links

subtract time from schedule to update task due date 

Calculate Date By Adding or Subtracting Time With A Schedule 

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

Community Alums
Not applicable

Thank you so much, it worked