- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 07:05 AM
Hi All,
I have created a scheduled job to run on last week wednesday of every month,but its failing to run for some month
var answer = false;
var rightNow = GlideDateTime();
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >= 23 && day <= 28) || (day >= 29 && day <= 31))){
answer = true;
}
answer;
Anyone can please help me on this.
Thanks,
Solved! Go to Solution.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 07:27 AM
Hi,
Your below condition
if (dayOfWeek == 3 && ((day >= 23 && day <= 28) || (day >= 29 && day <= 31))){
is equivalent to below
if (dayOfWeek == 3 && ((day >= 23 && day <= 31)){
This will create problem when 23rd and 30th both are Wednesday and if you plan to exclude 23 or 24 then it will create problem for Feb month.
Rather you can use below script with more logical condition:
var answer = false;
var rightNow = GlideDateTime();
var totalDaysInMonths=gdt.getDaysInMonthLocalTime()
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >(totalDaysInMonths-7)){
answer = true;
}
answer;
This might resolve your issue as well. I am assuming that your scheduled job is running daily.
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 07:20 AM
Hi Sangeetha,
Please refer and try the solution.
Get last weekday ("x") of month
var dayOfWeek_toLookFor = 3; // Wednesday
// Set the following to any date in the month you want to find the last
// Xday for. In this example, I'll find the last Wednesday in September
var dateInMonthToCheck = new Date(2017, 8, 1); // Note that 8 = September
// Here there be magic
if (!isNaN(dateInMonthToCheck.getTime())) {
var lastXDay = new Date(dateInMonthToCheck.getFullYear() +
(dateInMonthToCheck.getMonth() === 11 ? 1 : 0),
(dateInMonthToCheck.getMonth() + 1) % 12);
lastXDay.setDate(0); // Last day of previous month
lastXDay.setDate(lastXDay.getDate() -
(lastXDay.getDay() + 7 - dayOfWeek_toLookFor) % 7);
// Do stuff here with lastXDay
}
Mark Correct or Helpful if it helps.
***Mark Correct or Helpful if it helps.***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 07:27 AM
Hi,
Your below condition
if (dayOfWeek == 3 && ((day >= 23 && day <= 28) || (day >= 29 && day <= 31))){
is equivalent to below
if (dayOfWeek == 3 && ((day >= 23 && day <= 31)){
This will create problem when 23rd and 30th both are Wednesday and if you plan to exclude 23 or 24 then it will create problem for Feb month.
Rather you can use below script with more logical condition:
var answer = false;
var rightNow = GlideDateTime();
var totalDaysInMonths=gdt.getDaysInMonthLocalTime()
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >(totalDaysInMonths-7)){
answer = true;
}
answer;
This might resolve your issue as well. I am assuming that your scheduled job is running daily.
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 07:27 AM
Try this
var answer = false;
var rightNow = GlideDateTime();
var dayOfWeek = rightNow.getDayOfWeekLocalTime();
var day = rightNow.getDayOfMonthLocalTime();
if (dayOfWeek == 3 && ((day >= 23 && day <= 31))){
answer = true;
}
gs.info(answer);
Muhammad