set date field to display calculated value

tbofinger
Tera Contributor

I need to update a form so that a notification date field is updated to two weeks before the termination date field. The requestor sets the termination date, so I need a script to grab the termination date, subtract 14 days, and then publish the new date to the notification date field.

 

I’ve scripted the following Business Rule to complete this task, but it doesn’t work, and I’m unsure what I’m missing.

 

var grDate = current.u_disablementdate;

    grDate.addDaysUTC(-14);

    current.u_vendis = grDate;

 

Any thoughts or help would be greatly appreciated!

2 REPLIES 2

SwarnadeepNandy
Mega Sage

Hello @tbofinger,

 

  • First, you need to use the getGlideObject() method to convert the termination date field value into a GlideDateTime object, which you can then use the addDaysUTC() method on. Otherwise, you are trying to call a method on a string value, which will not work
  • Second, you need to use the setValue() method to update the notification date field value with the modified GlideDateTime object. Otherwise, you are assigning an object to a string field, which will not work either.
  • Third, you need to use the getDisplayValue() method to format the GlideDateTime object into a human-readable date string that matches the user’s time zone and locale settings. Otherwise, you may end up with a date string that is in UTC format and not user-friendly

Here is an example of how your script could look like after applying these suggestions:

 

 var grDate = current.u_disablementdate.getGlideObject(); // convert the termination date field value into a GlideDateTime object 
grDate.addDaysUTC(-14); // subtract 14 days from the GlideDateTime object 
current.setValue(‘u_vendis’, grDate.getDisplayValue()); // update the notification date field value with the formatted GlideDateTime object

 

 

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy

-O-
Kilo Patron
Kilo Patron

current.u_disablementdate is a GlideElement object which does not have a method called addDaysUTC.

While getGlideObject is a valid option in global scope,

var grDate = new GlideDateTime(current.u_disablementdate.toString());

is more "universal".

Assuming field u_vendis is a Date/Time one, making the above change would be enough for the script to work.

If field u_vendis is in fact not a Date/Time one, the correct thing would be to turn it into a Date/Time field. If one saves a date/time into a string field, it will only be OK for users who share time zone and format with the one saving the information.