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

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

This is scoped right if I remember most of your other questions?

If so, the solution of Vaibhav will work fine. Only the gs.print will not work, be aware of that.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

No need of Business rule you can pass the default value for the field to be something as below.

javascript:var gdt = new GlideDateTime().getDate();gdt.getByFormat('MMM - YYYY');

 

Thanks,

Jaspal Singh