
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 12:22 PM
From a UI Action, I am trying to set a date/time variable to the local date/time + 30 days but I can't get it to work. I'm testing this in a background script with this code:
var item = '39e27294db9598905a7c6d8d13961949';
var i = new GlideRecord('sc_req_item');
i.get(item);
var days = gs.getProperty('risk_request.expiration_days');
//this ^ is set to 30 and is integer type
gs.print(days);
var today = gs.nowDateTime();
var exp = new GlideDateTime(today);
gs.print('exp before addDays: ' + exp);
exp.addDays(days);
gs.print('exp after addDays: ' + exp);
i.variables.srr_expiration.setDisplayValue(exp);
i.update();
When I run this in a background script, I get these results:
*** Script: 30
*** Script: exp before addDays: 2020-06-05 15:04:31
*** Script: exp after addDays: 2020-07-05 15:04:31
Result looks good... but when it actually sets the variable value, it is set to today instead of
30 days from now.
I have tried so many variations of this, but nothing works... please help! What am I missing?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 12:38 PM
The gs.nowDateTime() doesn't work in all scenarios and scoped applications. The method now is to just use:
var today = new GlideDateTime() and that will set today to date/time right now. You can read more and find examples here:
So you code only needs to be:
var item = '39e27294db9598905a7c6d8d13961949';
var i = new GlideRecord('sc_req_item');
i.get(item);
var days = gs.getProperty('risk_request.expiration_days');
//this ^ is set to 30 and is integer type
gs.print(days);
var exp = new GlideDateTime();
gs.print('exp before addDays: ' + exp);
exp.addDays(days);
gs.print('exp after addDays: ' + exp);
i.variables.srr_expiration = exp;
i.update();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 12:38 PM
The gs.nowDateTime() doesn't work in all scenarios and scoped applications. The method now is to just use:
var today = new GlideDateTime() and that will set today to date/time right now. You can read more and find examples here:
So you code only needs to be:
var item = '39e27294db9598905a7c6d8d13961949';
var i = new GlideRecord('sc_req_item');
i.get(item);
var days = gs.getProperty('risk_request.expiration_days');
//this ^ is set to 30 and is integer type
gs.print(days);
var exp = new GlideDateTime();
gs.print('exp before addDays: ' + exp);
exp.addDays(days);
gs.print('exp after addDays: ' + exp);
i.variables.srr_expiration = exp;
i.update();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 12:51 PM
I could have sworn I tried that! But in any case, it works now. Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2020 12:42 PM
Hi,
Try like this.
You don't need gs.nowDatetime() fucntion. you can simply call new GlideDateTime() and it returns today date and time.
set the date like this.
i.variables.srr_expiration.setValue(exp);
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP