Set a date via a flow based on dynamic year

Bidduam
Tera Guru

I am trying to set a flow variable that I've called - next_audit_date

 

What I want to do is to set the value of the flow variable to be june 30 of the year that that date comes next on.

 

EG: if it is 20 may, then set date as June 30, current year (2025)

If it is 10 July, then set the date to be June 30 the next year (2026)

 

Anyone able to help please?

2 ACCEPTED SOLUTIONS

James Chun
Kilo Patron

Hi @Bidduam,

 

This may need bit of testing but try the following code to set your flow variable:

var nowGD =new GlideDate();
var year = nowGD.getYearNoTZ();
if( !((nowGD.getDayOfMonthNoTZ()) < 30 && (nowGD.getMonthNoTZ() <= 6)))
{
	year ++;
}

var auditGD = new GlideDate();
auditGD.setDisplayValue(year + '-06-15');

return auditGD;

View solution in original post

J Siva
Kilo Patron
Kilo Patron

Hi @Bidduam 
Try the below script.

var today = new GlideDateTime();
var currentYear = today.getYear();
var june30th = new GlideDateTime();
june30th.setValue(currentYear+'-06-30'); // set to a known date for comparison
gs.print(june30th);
if (today.before(june30th)) {
    var june30thThisYear = new GlideDateTime();
    june30thThisYear.setValue(currentYear + '-06-30');
    // set the custom variable value
   return june30thThisYear;
} else {
    var june30thNextYear = new GlideDateTime();
    june30thNextYear.setValue((currentYear + 1) + '-06-30');
    // set the custom variable value
    return june30thNextYear;
}

JSiva_0-1748238868913.png

Output:

JSiva_1-1748238889384.png

Regards,
Siva

View solution in original post

7 REPLIES 7

@J Siva 

Thank you, I had already just gotten a solution from @James Chun , however I have also tested yours and it also looks to work as I'd want it to, so I will also mark yours as a solution since ServiceNow now allows for more than one reply to be a solution.

 

Thank you very much for your reply and answer, I really appreciate always getting so much help in this forum 🙂

@Bidduam Glad it helped 🙂

SuyashJ41731830
Kilo Guru

Hi
@Bidduam 

(function execute(inputs, outputs) {
var today = new GlideDateTime();
var currentYear = parseInt(today.getYearLocalTime());

// Create a GlideDateTime for June 30 of current year
var june30 = new GlideDateTime(currentYear + "-06-30 00:00:00");

// If today is after June 30, set it for next year
if (today.after(june30)) {
currentYear += 1;
june30 = new GlideDateTime(currentYear + "-06-30 00:00:00");
}

// Set the flow output variable
outputs.next_audit_date = june30;

})(inputs, outputs);

If my response helped please mark it correct

Thanks, and Regards, 
Suyash