Add days to date field

oharel
Kilo Sage

Hi,

I have a date field on a form. When a new form is opened, I want to add 14 days to the field. The new date (the day the form was opened +14) should not change, unless the user changes it manually - that is, I don't want the date to be calculated anew every time the form is loaded.

Also, if the date is changed by the user, an alert should pop up saying something like "you chose to change the date".

1. Where should the calculation of the field be? in a BR? Or the field's variable (Personalize Dictionary)?

2. I tried something like this in a BR:

var gdt = new GlideDateTime(current.u_nowdate);   // If there's a better way to do it without this additional field u_nowdate, let me know (like gs.nowDateTime();)

gdt.addDays(14);  

current.u_starting_date = gdt;

current.update();

It does not work, of-course

And then I thought about adding a client script (this works, but for it to be effective, i need the field date to be set correctly):

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading) {

  return;

}

  return confirm('You changed the starting date from ' + oldValue + ' to ' + newValue + " " +'\n' + 'Are you sure you want to do that?');

}

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would set this as the default value in the dictionary entry for the field. You would just need javascript:gs.daysAgo(-14); in that field. Then you could do your client script on change.



b417819d37.png


View solution in original post

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would set this as the default value in the dictionary entry for the field. You would just need javascript:gs.daysAgo(-14); in that field. Then you could do your client script on change.



b417819d37.png


OK, I feel stupid now.


Thanks!


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Ahh no way. Dates are so tricky to work with in SN that it's easy to assume it's going to be more complicated.