Schedule a Flow to Run on the Last Monday of Every Month

jmiskey
Kilo Sage

I am trying to schedule a Flow to run on the last Monday of every month, but I am unsure of how to set that up.  Does anyone know how to do that? 

 

(And yes, it does actually have to be the last Monday of every month - there is no leeway there, so please do not offer any suggestions that do not meet that requirement.)

 

Thank you.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

you will have to make flow run daily, then create a custom flow action and check if today is last Monday of the month.

It will return true/false.

if true then your next steps run, if false then your flow ends

OR
You can use daily scheduled job and use Conditional script, something like this and you can enhance

add that script and set answer=true or false in this script

AnkurBawiskar_0-1753277501413.png

 

Schedule report to run on last Friday of every month 

Scheduled Job Condition - First Monday of Every Month 

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

7 REPLIES 7

Thank you.  That gave me the information I needed to complete this.

 

I first created a Custom Action to check if we were in the last week of the month, by adapting the code in the link.  The Script for that Custom Action looks like this:

(function execute(inputs, outputs) {
	var report_date = new GlideDateTime();
	var days_in_month = report_date.getDaysInMonthUTC();
	var day_of_month = report_date.getDayOfMonthUTC();
	if (day_of_month >= days_in_month - 6) {
		outputs.islastweekofmonth = true;
	} else {
		outputs.islastweekofmonth = false;
	}	
})(inputs, outputs);

 

And I set the Trigger of my flow to run at a certain time every Monday (every day not necessary, since I only want it to run on the last Monday of every Month).  And the first step of my flow as to call the Custom Action to return a True/False value indicating if where we in the last week of the month.  If that returns TRUE, it proceeds to create my Task.

 

Thanks for the help!

@jmiskey 

Glad to help.

Happy learning.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Unique45
Mega Sage

Hello @jmiske

As per my knowledge we cannot add such condition to flow.

Here is another way:

You can create a scheduled job and add the following script in the Condition field:

isLastMondayToday();
function isLastMondayToday() {
    var today = new GlideDateTime();
    var year = today.getYearLocalTime();
    var month = today.getMonthLocalTime();
    var lastMonday = getLastMonday(year, month);
    answer = today.getDate().getDisplayValue() == lastMonday.getDisplayValue();
}

function getLastMonday(year, month) {
    var gdt = new GlideDateTime();
    gdt.setDisplayValue(year + '-' + month + '-01');
    gdt.addMonthsUTC(1);
    gdt.addDaysUTC(-1); 

    while (gdt.getDayOfWeekUTC() != 1) {
        gdt.addDaysUTC(-1);
    }

    return gdt.getDate(); 
}

 

And in script call your flow.

 

 

Please mark correct/helpful if this helps you!