To add 4 years to todays year and subtract 1 month present month

Saikeerthana
Tera Contributor

I want update

valid_year = todays year + 4

valid_month=todays month-1

I wrote the following code, but it is not working

var gd = new GlideDateTime(gs.nowDateTime());
//gdt=gd.getYearLocalTime();
gr.u_valid_year = gd.addYearsLocalTime(4);
gr.u_valid_month = gd.addMonthsLocalTime(1);

Thanks in advance!

9 REPLIES 9

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Just running this code:

Evaluator: com.glide.script.RhinoEcmaError: "gr" is not defined.

First thing would be, fix this. Or are you not sharing your full code?

If my answer helped you in any way, please then mark it as helpful.

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

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

var gr = new GlideRecord('u_atm_card_cells');

gr.initialize();

if (!gr.next()) {
gr.newRecord();
gr.u_card_holder_name = current.sys_id;

var gdt = new GlideDateTime();
gr.u_valid_through_year = gdt.addYearsLocalTime(4);
gr.u_valid_through_month = gdt.addMonthsLocalTime(1);
gr.insert();

}

 

This is my code.I tried your solution but my fields are not getting updated.

Mark Roethof
Tera Patron
Tera Patron

This works fine:

var gdt = new GlideDateTime();
gdt.addYearsLocalTime(4);
gs.print(gdt);

var gdt = new GlideDateTime();
gdt.addMonthsLocalTime(1);
gs.print(gdt);

If my answer helped you in any way, please then mark it as helpful.

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

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Saikeerthana ,

 

You can use the below script:-   

 

var setYear = new GlideDateTime();

setYear.addYearsLocalTime(4);

var gdt = new GlideDateTime(setYear);

gdt.addMonthsLocalTime(-1);

gs.print(gdt);   
 
Output:-
find_real_file.png
 

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy