Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Timezone issue

Rachna S
Tera Guru

Hi

 

We tested the following script and it worked fine then:

 

var currentDate = new GlideDateTime();
// Set the date to the first day of the next month
currentDate.addMonths(1);
currentDate.setDayOfMonth(1);
// Subtract one day to get the last day of the current month
currentDate.addDays(-1);
while (currentDate.getDayOfWeek() === 6 || currentDate.getDayOfWeek() === 7) {
currentDate.addDays(-1);
}
var lastWorkingDayOfMonth = currentDate.getLocalDate();
var lastworkingday = lastWorkingDayOfMonth.getDayOfMonthUTC();
var reportdate = new GlideDate().getDayOfMonthUTC();
var isLastBusinessDay = (lastworkingday == reportdate);

var report_date = new GlideDateTime();
var day_in_week = report_date.getDayOfWeekUTC();
// 1-Monday, 2-Tuesday, 3-Wednesday, 4-Thursday, 5-Friday, 6-Saturday, 7-Sunday
var days_in_month = report_date.getDaysInMonthUTC();
var day_of_month = report_date.getDayOfMonthUTC();
var isLastDay = (day_of_month == days_in_month);
var isWeekend = (day_in_week == 6 || day_in_week == 7);

if ((isLastDay && !isWeekend) || isLastBusinessDay){
// Execute the flow logic here (the code you want to run on the last working day)
gs.log("Running flow on the last working day of the month");
// gs.print("Running flow on the last working day of the month");
runJob();
}
else {
// Optional: Reschedule the flow for the next working day
// Or, skip running the flow and log an error
gs.log("Skipping flow execution: Not the last working day.");
}


function runJob() {
 
try {

 
 
// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var result = sn_fd.FlowAPI.getRunner().subflow('global.time_sheets_for_current_month').inBackground().run();
//var outputs = result.getOutputs();

// Current subflow has no outputs defined.
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
 
}
 
It is supposed to trigger a subflow on the last business day of every month. IT did run correctly on the 30th of September. However, it also triggered subflow on the 1st of October.
 
Can someone please advise why that would have happened ?
 
Thank you
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Rachna S 

try this

-> your script was comparing only day numbers without month/year

-> compare full date

var currentDate = new GlideDateTime();
// Set the date to the first day of the next month
currentDate.addMonths(1);
currentDate.setDayOfMonth(1);
// Subtract one day to get the last day of the current month
currentDate.addDays(-1);
// Adjust backwards for weekends until last working day found
while (currentDate.getDayOfWeek() === 6 || currentDate.getDayOfWeek() === 7) {
    currentDate.addDays(-1);
}
var lastWorkingDayOfMonth = currentDate.getLocalDate();

// Format dates as yyyy-MM-dd for accurate comparison
var lastWorkingDayDateStr = lastWorkingDayOfMonth.getByFormat('yyyy-MM-dd');
var todayDate = new GlideDate();
var todayDateStr = todayDate.getByFormat('yyyy-MM-dd');

var report_date = new GlideDateTime();
var day_in_week = report_date.getDayOfWeekUTC();
// 1-Monday, 2-Tuesday, ..., 6-Saturday, 7-Sunday
var days_in_month = report_date.getDaysInMonthUTC();
var day_of_month = report_date.getDayOfMonthUTC();
var isLastDay = (day_of_month == days_in_month);
var isWeekend = (day_in_week == 6 || day_in_week == 7);

var isLastBusinessDay = (lastWorkingDayDateStr == todayDateStr);

if ((isLastDay && !isWeekend) || isLastBusinessDay) {
    // Execute the flow logic here (the code you want to run on the last working day)
    gs.log("Running flow on the last working day of the month");
    runJob();
} else {
    gs.log("Skipping flow execution: Not the last working day.");
}

function runJob() {
    try {
        // Execute Synchronously: Run in foreground
        var result = sn_fd.FlowAPI.getRunner().subflow('global.time_sheets_for_current_month').inBackground().run();
    } catch (ex) {
        var message = ex.getMessage();
        gs.error(message);
    }
}

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

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Rachna S 

why are you not using GlideSchedule in your calculation?

You can make scheduled job run daily and see if that

see below link and solution from dvp

Schedule job to run last working day of month 

AnkurBawiskar_0-1759327870955.png

 

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

@Rachna S 

Hope you are doing good.

Did my reply answer your question?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Rachna S 

try this

-> your script was comparing only day numbers without month/year

-> compare full date

var currentDate = new GlideDateTime();
// Set the date to the first day of the next month
currentDate.addMonths(1);
currentDate.setDayOfMonth(1);
// Subtract one day to get the last day of the current month
currentDate.addDays(-1);
// Adjust backwards for weekends until last working day found
while (currentDate.getDayOfWeek() === 6 || currentDate.getDayOfWeek() === 7) {
    currentDate.addDays(-1);
}
var lastWorkingDayOfMonth = currentDate.getLocalDate();

// Format dates as yyyy-MM-dd for accurate comparison
var lastWorkingDayDateStr = lastWorkingDayOfMonth.getByFormat('yyyy-MM-dd');
var todayDate = new GlideDate();
var todayDateStr = todayDate.getByFormat('yyyy-MM-dd');

var report_date = new GlideDateTime();
var day_in_week = report_date.getDayOfWeekUTC();
// 1-Monday, 2-Tuesday, ..., 6-Saturday, 7-Sunday
var days_in_month = report_date.getDaysInMonthUTC();
var day_of_month = report_date.getDayOfMonthUTC();
var isLastDay = (day_of_month == days_in_month);
var isWeekend = (day_in_week == 6 || day_in_week == 7);

var isLastBusinessDay = (lastWorkingDayDateStr == todayDateStr);

if ((isLastDay && !isWeekend) || isLastBusinessDay) {
    // Execute the flow logic here (the code you want to run on the last working day)
    gs.log("Running flow on the last working day of the month");
    runJob();
} else {
    gs.log("Skipping flow execution: Not the last working day.");
}

function runJob() {
    try {
        // Execute Synchronously: Run in foreground
        var result = sn_fd.FlowAPI.getRunner().subflow('global.time_sheets_for_current_month').inBackground().run();
    } catch (ex) {
        var message = ex.getMessage();
        gs.error(message);
    }
}

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

Thanks so much.