How to add Years to the "created date" to come up with a new Date with bus rule

fredfariello
ServiceNow Employee
ServiceNow Employee

 

Here is my bus rule script. I want to populate automatically the u_eol_date (date/time field) with the sys_created_on date (date/time field) PLUS the u_years_of_life value (integer).

 

Notice on the form results below, it works to some degree, though it is short by a day and the time is a bit off. Not a biggie but wondering how to get it right. Also is a bus rule the best way to do it?

 

Bus Rule Script

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var CreateDate = current.sys_created_on;
var WarYears = current.u_years_of_life;
var gDate = new GlideDate();
gDate.setValue(CreateDate);
var gDT = new GlideDateTime(gDate);
gDT.addYears(WarYears);
current.u_eol_date = gDT.getDate();

})(current, previous);

 

 

find_real_file.png

1 ACCEPTED SOLUTION

Joni5
Tera Expert

 

Your gDT.getDate() is taking just the date so it's '2014-08-11'. Then the hours are added due to this being a datetime field.

Try using current.u_eol_date = gDT.getValue();

I'd also change the new GlideDate() to new GlideDateTime() on gDate.

View solution in original post

2 REPLIES 2

Joni5
Tera Expert

 

Your gDT.getDate() is taking just the date so it's '2014-08-11'. Then the hours are added due to this being a datetime field.

Try using current.u_eol_date = gDT.getValue();

I'd also change the new GlideDate() to new GlideDateTime() on gDate.

fredfariello
ServiceNow Employee
ServiceNow Employee

Bingo! That works. Thank Joni!