- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2018 07:56 AM
Hello all,
I will appreciate if anyone can collaborate providing some ideas on how to add years to a Date Field (Certification Expiration) from a Date Field (Date Trained). I have tried the following but it is not working(Certification Expiration field stays blank after saving request):
1. Right click on the 'Valid Field'
2. Click on personalize Dictionary
3. On default Value tabs, write the script below (ensure that the 'Use dynamic default is unchecked) :
javascript: var dat = new GlideDate(); dat.setValue(gs.monthsAgo(-12));
dat.getDisplayValue();
or
javascript: var dat = new GlideDateTime('u_date_trained');
dat.setValue(gs.monthsAgo(-12));
dat.getDisplayValue();
or
javascript: var dat = new GlideDateTime(); dat.addYears(2);
dat.getDisplayValue();
4. Save
Again, I will greatly appreciate if anyone has an idea to make this calculation.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2018 09:18 AM
this code worked for me in before update business rule:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var d1 = new GlideDate();
d1.setValue(current.u_d1);
var yr = d1.getYearNoTZ();
yr = yr + 1;
var s1 = d1.getValue();
s1 = yr+s1.substring(4);
current.u_d2 = s1;
})(current, previous);
result:
Hope this helps.
Thanks,
Ali
Thank you,
Ali

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2018 09:13 AM
try below code in BR: before insert business rule
var d1 = new GlideDate();
d1.setValue(current.u_date_trained);
var yr = d1.getYearNoTZ();
yr = yr + 1;
var s1 = d1.getValue();
s1 = yr+s1.substring(4);
current.u_cert_expiration_date = s1;
Thank you,
Ali

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2018 09:18 AM
this code worked for me in before update business rule:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var d1 = new GlideDate();
d1.setValue(current.u_d1);
var yr = d1.getYearNoTZ();
yr = yr + 1;
var s1 = d1.getValue();
s1 = yr+s1.substring(4);
current.u_d2 = s1;
})(current, previous);
result:
Hope this helps.
Thanks,
Ali
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2018 09:31 AM
Thanks a lot Ali. Works great!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2018 06:48 AM
Hey Ahmmed,
Hope you are doing well. Wanted to ask if this can be done in an onChange client script to have the changes happen on client side. I ask because I also have to set a status to inactive depending on one of the dates (expiration date) to change the status. I have a few business rules to do this on the server side but are kind of glitchy.
One BR I set was to run when training date is less than 2 years before expiration date to set status to active and the other is when training date is more than 2 years before expiration date to set status to inactive.
When I run a request for a user the status sometimes doesnt change at all (stays blank) or if I change the training date to last week the expiration date changes correctly but the status changes to inactive which is not correct.
Will appreciate your help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2018 12:21 AM
Hello,
Apologies for delay in getting back to you.
if you want same functionality on the client side onChange script, then it can be done using GlideAjax.
have a script include created and have same logic as Business rule.
call the script include from client script and pass the trained date to the script include and in script include have same functionality as BR and return expiration date.
please refer below link for more details on GlideAjax.
https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference/r_ExamplesOfAsynchronousGlideAjax.html
Hope it is clear. Let me know for any difficulty.
Thanks,
Ali
Thank you,
Ali