Date xxx days from now

airwin77
Kilo Contributor

Anyone have any thoughts or examples of how to get ServiceNow to take any of the gs.(date records) like Now() and add say 2 days to it? What I'm attempting to to do is make it so that the default date that is presented on a request form shows a date that is at least 2 days in the future? Many of the other tools I support and work with I would just do something simple like Now()+2, however from a ServiceNow perspective all that yeilds is the value for today?

I've looked at http://wiki.servicenow.com/?title=GlideSystem and it has several examples of how to script time / hours in the past however it looks like they never thought to add anything that would be useful for days from now?

Likewise I already use scripts that can validate end time is greater than start time, however those really don't help me when it comes to setting a default value for a future date.

Thank in Advance

9 REPLIES 9

airwin77
Kilo Contributor

When I try the script using -5 as the value, I keep getting 09-12-0168 as the date? I've changed the field type and it didn't really make any difference. I've tried several numbers plus or negative and the results ar always very similar to the the date listed above? Maybe I'm missing the big Red truck here, but I'm not able to yield any positive results.


airwin77
Kilo Contributor

I'm trying the default script again to populate a date in the future, however nothing I try is working? The only response I was getting from the system kept showing the date as 1970? So I've been looking trying to find out what the format should be, and thus far I haven't found much that is current or relevant. I've read a couple articles that discuss the new API calls that were introduced in Calgary, if this is the reason that I'm not having any luck does anyone know how going forward to define these scripts so that a date/time field will populate a day say 2 days in the future?

If gs.daysAgoStart('-5') is still valid, what am I missing to get this to work correctly? I've been trying Calgary and Dublin releases.


airwin77
Kilo Contributor

Are you sure that gs.daysAgoStart(-5) is still valid in Calgary or Dublin? I can not get it to do anything? I've also tried with and without javascript: before the gs statement. Does the field type have to be something other than date/time if so what? I've tried a string and journal fields with no better results?

With Calgary I tried an instance with the General release version of Calgary, then I tried on antoher with the most recent hotfix & patch version, and I tried the all dublin instances up to the current hotfix 5.


TJW2
Mega Guru

Take a look at this post for syntax.

"gs.daysAgo(-1)" not evaluating correctly


airwin77
Kilo Contributor

Here is what I finally got to work for me as a Business Rule
Active: X
When: before
Insert: X
Update: X

current.getValue('u_disable_auto_date') == 0 (Because I have this Business Rule set to run this script on either inserts or updates, I needed a way to disable the script if I just want to add a comment to the task.

I then have a True/False check box on the form, which is what the condition is referencing.
Any condition that you need to be true first (I have one that looks to make sure another field is 0 (false)

Script:
current.Your Field Name.setValue(gs.daysAgo(-90)) ;
Change the -90 to reflect the amount of time from now that you want this set as, negative values end up adding days.

You can also take the script and tweak it like I did with some if statements:
if (current.operation() == 'update') {
// If no due date was specified, calculate a default
if (current.u_next_required_scan == '') {

if (current.u_normal_user_bb == '1') {
// Set due date 180 days from now
current.u_next_required_scan.setValue(gs.daysAgo(-180)) ;
}

if (current.u_special_bb == '1') {
// Set due date to 90 days from now
current.u_next_required_scan.setValue(gs.daysAgo(-90)) ;
}
}
}

Again on the form I just have 2 true / false options which is what the script uses to determine 90 or 180 days. If you go this route make sure to set a UI Policy so that a user can't select both.

*** Hope this helps those of you with a similar issue.