The CreatorCon Call for Content is officially open! Get started here.

Setting variables for Mondays in month

cgedney
Giga Guru

I've been asked to create automated tasks at the beginning of the month for the 4 or 5 Mondays in the month. I am triggering on the first of the month and I created 5 variables (firstMonday, secondMonday, etc.). I am setting the firstMonday with the following code:

var dt = new GlideDateTime();
dt.setDisplayValue(fd_data.trigger.run_start_time.getDisplayValue());

dt.setDate(1); //Start on the first of the month, in case I run it outside of schedule

// Get the first Monday in the month
while (dt.getDay() !== 1) {
  dt.setDate(dt.getDate() + 1);
}

return dt.getDisplayValue();

I am doing the same thing, except I add a "dt.setDate(dt.getDate() + 7);" for each day I want to get to. The code works fine when I run it outside of SN. When I run it in SN, it just sits there and then cancels after about 5 minutes with an Internal Server Error. The error message says it needs to return something, but I am return the date. What am I missing?

 

Thank you, Charles

1 ACCEPTED SOLUTION

folusho
Tera Guru

@cgedney 

Please try this.

 

In ServiceNow, GlideDateTime.setDate() does not increment the date like you’re trying to do (e.g., dt.setDate(dt.getDate() + 1) is not valid GlideDateTime logic). Instead, you need to use addDaysLocalTime() to move the date forward.

 

(function getMondaysOfMonth() {
    var runTime = new GlideDateTime();
    runTime.setDisplayValue(fd_data.trigger.run_start_time.getDisplayValue());

    // Reset to first day of the current month
    var month = runTime.getLocalDate().getMonthLocalTime() + 1; // months are 0-based
    var year = runTime.getLocalDate().getYearLocalTime();
    var dt = new GlideDateTime(year + '-' + month + '-01 00:00:00');

    // Move forward to the first Monday
    while (dt.getDayOfWeekLocalTime() !== 1) { // 1 = Monday
        dt.addDaysLocalTime(1);
    }

    // Store up to 5 Mondays
    var mondays = [];
    for (var i = 0; i < 5; i++) {
        // Only push if the date is still in the same month
        var currentMonth = dt.getLocalDate().getMonthLocalTime();
        if (currentMonth === month - 1) {
            mondays.push(dt.getDisplayValue());
            dt.addDaysLocalTime(7);
        } else {
            break; // Exit loop if we move into the next month
        }
    }

    // Log or return the values (adapt this part based on your use case)
    gs.info('Mondays this month: ' + mondays.join(', '));

    // Example: return the first Monday
    return mondays[0];

})();

 

View solution in original post

2 REPLIES 2

folusho
Tera Guru

@cgedney 

Please try this.

 

In ServiceNow, GlideDateTime.setDate() does not increment the date like you’re trying to do (e.g., dt.setDate(dt.getDate() + 1) is not valid GlideDateTime logic). Instead, you need to use addDaysLocalTime() to move the date forward.

 

(function getMondaysOfMonth() {
    var runTime = new GlideDateTime();
    runTime.setDisplayValue(fd_data.trigger.run_start_time.getDisplayValue());

    // Reset to first day of the current month
    var month = runTime.getLocalDate().getMonthLocalTime() + 1; // months are 0-based
    var year = runTime.getLocalDate().getYearLocalTime();
    var dt = new GlideDateTime(year + '-' + month + '-01 00:00:00');

    // Move forward to the first Monday
    while (dt.getDayOfWeekLocalTime() !== 1) { // 1 = Monday
        dt.addDaysLocalTime(1);
    }

    // Store up to 5 Mondays
    var mondays = [];
    for (var i = 0; i < 5; i++) {
        // Only push if the date is still in the same month
        var currentMonth = dt.getLocalDate().getMonthLocalTime();
        if (currentMonth === month - 1) {
            mondays.push(dt.getDisplayValue());
            dt.addDaysLocalTime(7);
        } else {
            break; // Exit loop if we move into the next month
        }
    }

    // Log or return the values (adapt this part based on your use case)
    gs.info('Mondays this month: ' + mondays.join(', '));

    // Example: return the first Monday
    return mondays[0];

})();

 

cgedney
Giga Guru

Thank you folusho, that seems to have done the trick.