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

Ahmmed Ali
Mega Sage

Hi,

 

try below code. It worked for me.

var d1 = new GlideDate();
gs.print(d1);

var yr = d1.getYearNoTZ();
yr = yr + 1;
var s1 = d1.getValue();
s1 = yr+s1.substring(4);

d1.setValue(s1);

gs.print(d1);

 

output:

*** Script: 2018-09-14
*** Script: 2019-09-14

Not sure if this is best way but it works

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

Hello Ali,

I added this into a Business Rule and tried to get the value to the Certification Expiration Date (u_cert_exp_date) and it stays blank.  Cannot use on Client Script since cannot use gs.  Can you show me how to get the value to show on the field?

Hello,

 

please let me know the field types you are using.

 

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

u_date_trained is Date as well as u_cert_expiration_date.

Thanks!