Scheduled script execution to run on quarterly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 02:11 PM
Hello all,
I am trying to create a scheduled script execution which should generate a record 10 business days prior to the due date. Here the due date is the 1st business day after the end of the quarter.
Can someone help me on how to achieve this condition?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 07:05 PM
Hi @deepum
Try conditional schedule script like the one below with the script.
var answer = false;
var gdt = new GlideDateTime();
var gd = gdt.getLocalDate();
gdt.setDisplayValueInternal( gd + " 09:00:00");
var current_month = parseInt(gdt.getMonthLocalTime());
if(current_month == 3 || current_month == 6 || current_month == 9 || current_month == 11){
//find first business day in the next month
var found = false;
var year = parseInt(gdt.getYearLocalTime());
var month = current_month + 1;
if(month == 13){
month = 1;
year = year + 1;
}
var day = 1;
var first_bday;
while(!found){
var date = year + "-" + month + "-" + day;
var nbdSt = new GlideDateTime();
nbdSt.setDisplayValueInternal( date + " 09:00:00");
var nbdEn = new GlideDateTime();
nbdEn.setDisplayValueInternal( date + " 16:00:00");
var dur = new DurationCalculator();
// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.calcScheduleDuration(nbdSt, nbdEn);
var secs = dur.getSeconds();
if(secs > 14400){
found = true;
first_bday = nbdSt;
}else{
day +=1;
}
}
var dur = new GlideDuration(60 * 60 * 1000 * 10 * 8);
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //put your schedule sys_id here
var tbDay = schedule.add(gdt, dur);
if(tbDay.getDayOfMonthLocalTime() == first_bday.getDayOfMonthLocalTime()){
answer = true;
}
}
answer;
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 07:55 AM
Thank you Anvesh. I am trying to understand why did you use 14400 seconds and also you used month==11 in line 9? Can you please let me know why
Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 06:48 PM
Hi @deepum
I forgot to change those two values as I set them for testing. It should be 12 instead of 11. And the 14400 can be anything greater than 1.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 09:24 AM
HI @deepum ,
I trust you are doing great.
Here's a sample script to guide you through this process. Note that this script uses a simplified method for calculating business days and might need adjustments based on specific business calendar and holiday schedules.
function calculateDueDate() {
var today = new GlideDate();
var year = today.getYear();
var quarterEnd = new GlideDate();
// Determine the end of the current quarter
if(today.getQuarter() == '1') {
quarterEnd.setValue(year + '-03-31');
} else if(today.getQuarter() == '2') {
quarterEnd.setValue(year + '-06-30');
} else if(today.getQuarter() == '3') {
quarterEnd.setValue(year + '-09-30');
} else {
quarterEnd.setValue(year + '-12-31');
}
// Calculate the first business day after the end of the quarter
var firstBusinessDay = new GlideDateTime(quarterEnd);
do {
firstBusinessDay.addDays(1);
} while (isWeekendOrHoliday(firstBusinessDay));
// Calculate 10 business days prior
var targetDate = new GlideDateTime(firstBusinessDay);
var counter = 10;
while(counter > 0) {
targetDate.addDays(-1);
if (!isWeekendOrHoliday(targetDate)) {
counter--;
}
}
return targetDate;
}
function isWeekendOrHoliday(glideDateTime) {
var day = glideDateTime.getDayOfWeek();
var isWeekend = (day == 6 || day == 0); // Assuming 0 is Sunday and 6 is Saturday
var isHoliday = checkHoliday(glideDateTime); // Implement checkHoliday function based on your holiday calendar
return isWeekend || isHoliday;
}
// Implement a function to check for holidays
function checkHoliday(glideDateTime) {
// Your holiday logic here
return false; // Placeholder, return true if it's a holiday
}
// Schedule this script to run daily and check if today is the target date
var targetDate = calculateDueDate();
if (new GlideDateTime().getLocalDate().equals(targetDate.getLocalDate())) {
// Logic to create a record
// e.g., create a new incident, task, etc.
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi