How to get 2 business day before a selected date?

Dominique3
Mega Contributor

Hi, 

I'm trying to do a schedule to run every 2nd business day before the 20th of every month, excluding holidays. I have tried a few things, but none of them worked.

Can anyone help me with this?

Thanks!

1 ACCEPTED SOLUTION

Just to correct the Schedule script, I had to use a getDate() in my condition to run or not, as in my case I didn't need the hours, and a toString(), as it was not considering the date as a number. The script looks like this now, and it works perfectly (don't forget to use the script include to test if it is a business day or not):

 

var today = new GlideDateTime();
var start_day = new GlideDateTime();
start_day.setDayOfMonthUTC(15);
//gs.print(start_day.toString());
var count_business_days = 0;
var maxInt = 31;

while (count_business_days < 2 && maxInt > 0) {
maxInt = maxInt - 1;
if (new IsGivenDayBusinessDay().execute(day = start_day.toString(), calendar = 'Global HUB', gmt = -3)) {
count_business_days = count_business_days + 1;
}
start_day.addDaysUTC('-1');
}
//gs.print(start_day.getDate().toString());
//gs.print(maxInt);

if (today.getDate().toString() == start_day.getDate().toString()) { 
true;
} else {
false;
}

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

there is a solution here; you cannot directly get it just like we can add days using schedule

How can I subtract time from a date? The Schedule class ignores negative durations.

Regards
Ankur

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

Hi Ankur,

 

Thank you very much for your help!

I tried using this code to achieve it, but it didn't work very well for me.

What did work, with a friend's help, was to use a script include to verify if it's a business day, and a script on my schedule to get the desired day.

If anyone needs it, you can try as bellow:

 

 

Script Include:

var IsGivenDayBusinessDay = Class.create();

IsGivenDayBusinessDay.prototype = Object.extendsObject(AbstractAjaxProcessor, {

execute: function(day, calendar, gmt) {

var answer = false;
var now = new GlideDateTime(day);
now.addSeconds(gmt * 60 * 60);
var year = now.getYearUTC();
var dayOfWeek = now.getDayOfWeekUTC();
var month = now.getMonthUTC();
var businessDays = [];
var holidays = [];
var arrayUtil = new ArrayUtil();

//Get Calendar ID
var gr = new GlideRecord('sys_calendar');
gr.addEncodedQuery('name=' + calendar.toString());
gr.query();
while (gr.next()) {
calendarID = gr.getValue('sys_id');
//gs.print(calendarID);
}

//Get the calendar business days
gr = new GlideRecord('sys_cal_day');
gr.addEncodedQuery('parent_id=' + calendarID);
gr.query();
while (gr.next()) {
businessDays.push(gr.getValue('day_of_week'));
}
//gs.print(businessDays.join(','));

//Get the calendar Holidays
gr = new GlideRecord('sys_holiday');
gr.addEncodedQuery('parent_id=' + calendarID);
gr.query();
while (gr.next()) {
holidays.push(gr.getValue('date'));
}
//gs.print(holidays.join(','));

if (arrayUtil.contains(businessDays, now.getDayOfWeekUTC())) {
if (!(arrayUtil.contains(holidays, now.getDate()))) {
answer = true;
}
}
//gs.print(answer);
return answer;
},

type: 'IsGivenDayBusinessDay'
});

 

Scheduled job script:

var today = new GlideDateTime();
var start_day = new GlideDateTime();
start_day.setDayOfMonthUTC(15); //here you set the day of the month you to start counting from, for example, I need 2 days before day 15, so I set it for 15.
//gs.print(start_day.toString());
var count_business_days = 0; //will set the number of days you want to count on the while loop.
var maxInt = 31; //variable just for preventing infinite loop.

while (count_business_days < 2 && maxInt > 0) {
maxInt = maxInt - 1;
if (new IsGivenDayBusinessDay().execute(day = start_day.toString(), calendar = 'Global HUB - No Holidays', gmt = -3)) {
count_business_days = count_business_days + 1;
}
start_day.addDaysUTC('-1'); //to count backwards.
}
//gs.print(start_day.toString());
//gs.print(maxInt);

if (today == start_day) {
true;
} else {
false;
}

 

Best regards,

Just to correct the Schedule script, I had to use a getDate() in my condition to run or not, as in my case I didn't need the hours, and a toString(), as it was not considering the date as a number. The script looks like this now, and it works perfectly (don't forget to use the script include to test if it is a business day or not):

 

var today = new GlideDateTime();
var start_day = new GlideDateTime();
start_day.setDayOfMonthUTC(15);
//gs.print(start_day.toString());
var count_business_days = 0;
var maxInt = 31;

while (count_business_days < 2 && maxInt > 0) {
maxInt = maxInt - 1;
if (new IsGivenDayBusinessDay().execute(day = start_day.toString(), calendar = 'Global HUB', gmt = -3)) {
count_business_days = count_business_days + 1;
}
start_day.addDaysUTC('-1');
}
//gs.print(start_day.getDate().toString());
//gs.print(maxInt);

if (today.getDate().toString() == start_day.getDate().toString()) { 
true;
} else {
false;
}