How to add years to a Date Field Type from Start Date

lrossy31
Tera Expert

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.

 

1 ACCEPTED SOLUTION

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:

find_real_file.png

Hope this helps.

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

12 REPLIES 12

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;

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

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:

find_real_file.png

Hope this helps.

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Thanks a lot Ali.  Works great!

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.

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

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali