how to set a Future date as 1 Year plus based on a date item set.

White Wolf
Tera Contributor

I am wanting to have an End date for 1 year plus *based on a date*, set within a form.

 

e.g. Start date 1-1-2015 (set by users\ Customer)

End date (Start date+ 1 year, dynamically updating based on Start date value change

 

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@White Wolf Please refer to this thread https://www.servicenow.com/community/developer-forum/add-an-year-to-a-current-date-in-client-script/... similar implementation has been done by the author in this thread.

I have an simpler solution than that, that seems to use the created date. For my need I need to have an option for Date value X plus 1 year

 

i also need to have the validation re run automatically when the reference value is changed

Rahul Kumar17
Tera Guru

Hi White,

 

To set the End date dynamically in ServiceNow based on a Start date field value, you can create a business rule that runs when the Start date field changes. Here are the steps to create the business rule:

  1. Go to the "Business Rules" module in ServiceNow.
  2. Click "New".
  3. Enter a name for the business rule, such as "Calculate End Date".
  4. Set the "Table" to the table where your form is located (e.g. "Incident").
  5. Set the "When to run" field to "Before".
  6. Set the "When" field to "insert, update".
  7. Under "Advanced", set the "Script" field to the following JavaScript code:

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

// Get the start date and calculate the end date
var startDate = current.start_date;
var endDate = new GlideDateTime(startDate);
endDate.addYears(1);

// Set the end date value on the current record
current.end_date = endDate.getDisplayValue();

})(current, previous);

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar