How to convert Duration field to date time field and calculate the diff on incident form

sony8
Tera Contributor

I have a requirement as below where need to check difference between two different type fields

field 1: duration field 

field 2 : duration field

field 3: data/time field

Need to calculate below equation:

field_1 = difference (field_2 -field_3)

I am not able to find solution since not understanding how to convert duration field(field 2) to date/time for getting duration of field 1 

I have written business rule for this.

    var gtime = new GlideDateTime(current.field_3); // date/time field 
    var dur = new GlideDateTime(current.field_2); // not getting how to convert to date/time
    var dur1 = GlideDateTime.subtract(gtime, dur); //the difference between gdt3 and gdt2
    current.field_1 =dur1;
    current.update();

 

please help me with above calculation. 

Thanks in Advance..

1 ACCEPTED SOLUTION

Hi,

share this

var gtime = new GlideDateTime(current.field_3).getNumericValue();
var dur = current.field_2.dateNumericValue(); // this should give milliseconds value
current.field_1.setDateNumericValue(dur-gtime);
current.update();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @sony 

update script as this

var gtime = new GlideDateTime(current.field_3).getNumericValue();
var dur = current.field_2.dateNumericValue();
current.field_1.setDateNumericValue(dur-gtime);
current.update();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

getting some false values as below

i have tried using above script but

field_1 is not getting accurate values instead the values are random

could you please let me know for any changes for the same script

Hello Sony,

As per the script provided by @Ankur Bawiskar the value should be calculated correctly. Could you please provide the details of of is the value in your field1, field2 and field3. It is not easy to guess from the screenshot which is your field1, field2 and field3 and what data you are looking for in these 3 fields.

Thanks

Hi,

did you add gs.info() to debug?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader