- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2023 02:02 PM - edited 05-31-2023 02:19 PM
Hi all,
I want to send report on Last business day of Month, kindly help me how to fix this issue.
var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Check Business day, Exculde Holidays and Weekends //
var d = new GlideDateTime('2023-04-12');
var total_days = d.getDaysInMonth();
var abc = false;
var last_business_day = '';
d.setDayOfMonth(total_days);
while(!abc){
abc = sched.isInSchedule(d);
if(!abc){
d.addDays(-1);
}
}
var business_day = new GlideDateTime(d).getDate();
gs.info('business_day : '+business_day);
Here I have taken 'April 2023' month , as per above script I'm getting Last working day is 'April 29th' , but April 29th is Saturday.
Please help me how to get right Last business Day of Month. what is wrong in my code.?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 07:00 AM
@Supriya25 Use this updated code for both current date and future date testing.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2023 04:27 PM
Hi,
I tried your script, it runs for ever, so I tried:
var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Check Business day, Exculde Holidays and Weekends //
var d = new GlideDateTime('2023-04-12');
gs.info("Starting with d = " + d);
var total_days = d.getDaysInMonth();
var abc = false;
var last_business_day = '';
d.setDayOfMonth(total_days);
gs.info("total days = " + total_days + ", checking date d=" + d);
var loopCount = 0;
while(!abc){
abc = sched.isInSchedule(d);
gs.info("abc = " + abc);
if(!abc) {
d.addDays(-1);
gs.info("going back a day, d = " + d);
}
loopCount++;
if (loopCount > 5)
break;
}
var business_day = new GlideDateTime(d).getDate();
gs.info('business_day : '+business_day);
and got:
*** Script: Starting with d = 2023-04-12 00:00:00
*** Script: total days = 30, checking date d=2023-05-01 00:00:00
*** Script: abc = false
*** Script: going back a day, d = 2023-04-30 00:00:00
*** Script: abc = false
*** Script: going back a day, d = 2023-04-29 00:00:00
*** Script: abc = false
*** Script: going back a day, d = 2023-04-28 00:00:00
*** Script: abc = false
*** Script: going back a day, d = 2023-04-27 00:00:00
*** Script: abc = false
*** Script: going back a day, d = 2023-04-26 00:00:00
*** Script: abc = false
*** Script: going back a day, d = 2023-04-25 00:00:00
*** Script: business_day : 2023-04-25
So it seems 'abc' is never getting set to true with your logic, need to see why 'sched.isInSchedule(d);' is not working as expected. For me, the schedule with sys_id = '08fcd0830a0a0b2600079f56b1adb9ae' is my "8-5 weekdays" schedule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 05:20 AM
sys_id = '08fcd0830a0a0b2600079f56b1adb9ae' is "8-5 weekdays" (exclude holidays and weekends)
So please suggest the solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 06:10 AM
@Supriya25 100% working code:
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 06:33 AM
Thanks for your reply,
what is the issue on my code please? can you guide me