Set string field to current month and year

JJG
Kilo Guru

Hello,

I have a string field called 'month'. I would like to default value to be set to the current month and year.

For example: If I inserted a record today, the 'month' field should be April - 2020

I figure that ill need to create a before insert business rule to achieve this but I do not know how to get only the year and month. Any help is appreciated.

1 ACCEPTED SOLUTION

Vaibhav
Mega Guru

Hi 

 

Try this

// Execute this at "SCRIPTS- BACKGROUND" for test purposes.

var gDate = new GlideDateTime().getDate();
gs.print(gDate.getByFormat('MMMMM - YYYY'));

For default Value use this : 

// This script line is useful for default value

javascript: new GlideDateTime().getDate().getByFormat('MMMMM - YYYY');

 

Mark it correct and helpful if it helps you

Regards,
Vaibhav Chaudhari

View solution in original post

6 REPLIES 6

Vaibhav
Mega Guru

Hi 

 

Try this

// Execute this at "SCRIPTS- BACKGROUND" for test purposes.

var gDate = new GlideDateTime().getDate();
gs.print(gDate.getByFormat('MMMMM - YYYY'));

For default Value use this : 

// This script line is useful for default value

javascript: new GlideDateTime().getDate().getByFormat('MMMMM - YYYY');

 

Mark it correct and helpful if it helps you

Regards,
Vaibhav Chaudhari

Wont that get me the month as a number? I need it as a string 

ex: not 04, but instead as 'April'

Hi JJG

Have you tried this? 

this line code giving month as "April" instead of "04".

Exactly what do you want?

 

 

Look at this

find_real_file.png

Hi,

use this in the before insert BR

var year = new GlideDateTime().getYear();

var gd = new GlideDate();

var monthName = gd.getByFormat("MMMM");

current.<string_field> = monthName + ' - ' + year;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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