Script for Last day of the current month in Flow Designer

Dave Wolfe
Tera Contributor

We are creating a flow (in Flow Designer) that will run on the first day of each month and will have multiple tasks attached for performing Preventative Maintenance on various pieces of equipment. We need to set the Due Date field to the last day of the month (this will be run on the 1st of each month). My scripting attempts have not worked. This should be pretty easy, but scripting is not my forte. 

 

Can anyone enlighten me? 

 

#FlowDesigner

1 ACCEPTED SOLUTION

Dharma Liyanage
Tera Guru

Hi @Dave Wolfe ,

I created an asset task with Due Date = End of Month date and tested this in a FLOW that is configured to execute on the first of each month. You may Try & See 🙂

 

DharmaLiyanage_0-1741040460239.png

 

var date = new Date();
var monthOffset = date.getDate() > 21 ? 2: 1;
var lastday =  new Date(date.getFullYear(), date.getMonth() + monthOffset, 0);
var gDate = new GlideDate(lastday);
var formattedDate = (gDate.getByFormat('yyyy-MM-dd HH:mm:ss'));
return formattedDate;
 
DharmaLiyanage_0-1741040916580.png

 

 

 

View solution in original post

3 REPLIES 3

Kieran Anson
Kilo Patron

You should be able to use a transform function on a date/time pill to do this (last option in the docs page link below)

 

https://www.servicenow.com/docs/bundle/yokohama-build-workflows/page/administer/flow-designer/refere...

 

For the date pill, you can either set a flow variable to the current date/time or based off of the trigger date time. 

 

If you need to go down the flow variable route the following should return the current date time 

 

return new GlideDateTime()

Dharma Liyanage
Tera Guru

Hi @Dave Wolfe ,

I created an asset task with Due Date = End of Month date and tested this in a FLOW that is configured to execute on the first of each month. You may Try & See 🙂

 

DharmaLiyanage_0-1741040460239.png

 

var date = new Date();
var monthOffset = date.getDate() > 21 ? 2: 1;
var lastday =  new Date(date.getFullYear(), date.getMonth() + monthOffset, 0);
var gDate = new GlideDate(lastday);
var formattedDate = (gDate.getByFormat('yyyy-MM-dd HH:mm:ss'));
return formattedDate;
 
DharmaLiyanage_0-1741040916580.png

 

 

 

That worked perfectly. Thank you!