The CreatorCon Call for Content is officially open! Get started here.

GlideDateTime.subtract doesn't work

abrouf
Kilo Sage

I used the following commands on default value to calculate the number of days subtracting current date to due date but didn't work. Any comment or advise is greatly appreciated.

javascript:calendar_duration = GlideDateTime.subtract(due_date.getDisplayValue(), gs.nowDateTime());

Following this syntax:

<duration_field> = GlideDateTime.subtract(<start_field>.getDisplayValue(),gs.nowDateTime());

21 REPLIES 21

Hi Rouf


Check the below thread where user was successfully able to use the calculated field option. This may be helpful.



How to set Calculated Value for Duration (or any other date/time) field


have you verified that it's running?   do you have a business rule queuing the event problem.updated?


I tested this way: no errors.


find_real_file.png


Mohammed


Please try this script, you need to make your duration field(your required field) a calculated value



Script:


(function calculatedFieldValue(current) {



var startDate = new GlideDateTime(current.due_date); // your required feild


//var endDate = new GlideDateTime(current.closed_at);


var today = new GlideDateTime();



var result = GlideDateTime.subtract(startDate, today);


return result;



Note: I believe you have a due date and you want to make your "duration" field   a calculated value??



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

No luck. I used this. I changed the field name with type Date for both.


(function calculatedFieldValue(current) {


var startDate = new GlideDate(current.u_rcaduedate); // your required feild


//var endDate = new GlideDateTime(current.closed_at);


  var today = new GlideDate();


  var result = GlideDateTime.subtract(startDate, today);


  return result;


})(current);


Getting this errors from "Script-Background"


Evaluator: org.mozilla.javascript.EcmaError: Cannot read property "u_rcaduedate" from null Caused by error in script at line 2 1: (function calculatedFieldValue(current) { ==> 2: var startDate = new GlideDate(current.u_rcaduedate);