Error using new GlideDateTime.subtract()

e_wilber
Tera Guru

I am trying to use the following code to get the duration between when the record was created and NOW (to get open-to-resolve duration).

(function executeRule(current, previous /*null when async*/) {
    current.u_time_to_resolve = new GlideDateTime.subtract(current.sys_created_on, new GlideDateTime().getDisplayValue());
})(current, previous);

I am getting this error when running this business rule though. Any idea what is happening? This is a scoped app.

Error running business rule 'Record Time to Resolve' on sn_customerservice_customer_care: CS0001798, exception: org.mozilla.javascript.EvaluatorException: Can't find method com.glide.glideobject.GlideDateTime.subtract(com.glide.script.glide_elements.GlideElementGlideObject,string). (sys_script.82d753881b7c91506fc6a8a8b04bcb9a.script; line 3)

6 REPLIES 6

Aman Kumar S
Kilo Patron

Update as is:

current.u_time_to_resolve = new GlideDuration(GlideDateTime.subtract(current.sys_created_on, new GlideDateTime().getDisplayValue()));

Best Regards
Aman Kumar

Ahmed Drar
Tera Guru
Tera Guru

Hello, the script throws an error because you trying to subtract glide object from string. 

GlideDateTime.subtract expects 2 GlideDateTime parameters  

try to follow the example below.

 

var gdt1 = new GlideDateTime("2011-08-28 09:00:00");
var gdt2 = new GlideDateTime("2011-08-31 08:00:00");
 
var dur = GlideDateTime.subtract(gdt1, gdt2); // Difference between gdt1 and gdt2
gs.info(dur.getDisplayValue());

Which of my paramaters would be considered a string?

sys_created_on should be a date and time, and newGlideDateTime should be the same thing.

 

new GlideDateTime().getDisplayValue() is consider a string . you can intialise gdt1 and gdt2 with a string

var gdt1 = new GlideDateTime(new GlideDateTime().getDisplayValue());
var gdt2 = new GlideDateTime(current.sys_created_on.getDisplayValue());
 
var dur = GlideDateTime.subtract(gdt1, 
	gdt2); // Difference between gdt1 and gdt2