addMonthsUTC on a GlideDateTime object is returning "undefined"

Nisar2
Mega Guru

Hi everyone,

 

Completely new to the ServiceNow platform and in the process of undergoing training. So bear with me if this is too basic or obvious.

I'm on Madrid build (Personal Developer instance). In one scenario that I'm working on, the requirement is to set the expiry date to 1 year from today whenever a new record is created. So basically, this is my action plan:

  1. Create a "after" business rule to run on "Insert"
  2. In the scripting part, the code I'm using is as follows:
var currentTime = new GlideDateTime();
current.u_allocated_from = currentTime; // this works as intended as the field gets populated with the current date/time

var monthsToAdd = parseInt(gs.getProperty('AllocationExpiry'), 10);
gs.info('Months to add is ' + monthsToAdd); // this works fine as well as I see "12" being printed in the logs

var addTime = currentTime.addMonthsUTC(monthsToAdd);
gs.info('future time is: ' + addTime); // <--- problem area: prints "undefined" in the logs

current.u_allocated_till =addTime; // thus, this fails too
current.update();

 

Any idea why addMonthsUTC doesn't work?

 

Thanks,

Nisar

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Nisar,

Are you using scoped app?

Also addMonthsUTC() doesn't return anything

remove that and it should work

remove this

var addTime = currentTime.addMonthsUTC(monthsToAdd);

and update it as below

currentTime.addMonthsUTC(monthsToAdd);

gs.info('future time is: ' + currentTime);

Regards

Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Nisar,

Are you using scoped app?

Also addMonthsUTC() doesn't return anything

remove that and it should work

remove this

var addTime = currentTime.addMonthsUTC(monthsToAdd);

and update it as below

currentTime.addMonthsUTC(monthsToAdd);

gs.info('future time is: ' + currentTime);

Regards

Ankur

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

Yep, you were right. The problem was trying to use a return value when there was none.

Alikutty A
Tera Sage

You can try this script, The addTime wont return any value when dot walked.

var currentTime = new GlideDateTime();
current.u_allocated_from = currentTime; // this works as intended as the field gets populated with the current date/time

var monthsToAdd = parseInt(gs.getProperty('AllocationExpiry'), 10);
gs.info('Months to add is ' + monthsToAdd); // this works fine as well as I see "12" being printed in the logs

currentTime.addMonthsUTC(monthsToAdd);

current.u_allocated_till =currentTime.getDate();
current.update();