Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Use Flow Designer to calculate duration between two times?

JP-ODU
Tera Guru

I've been asked to find a way to:

 

1. Record the time an assignment group first changes on an incident

2. Calculate the difference between that time and the time the incident was created

 

To do this, I tried the following:

 

1. Added field u_first_assignment_change (time/date type)

2. Added field u_time_until_first_assigned (duration type)

3. I built a workflow to run once when Incident is updated and assignment group changes

4. The first step updates the incident record so u_first_assignment_change=Trigger->Record Updated->Run Start Date/Time

 

1-4 is all working successfully

 

But then I try

 

5. Add a second step to update the incident record, field Time Until Assigned with script:

var gdt1 = new GlideDateTime(current.sys_created_on.getDisplayValue());

var gdt2 = new GlideDateTime(gs.nowDateTime());

var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2

var dur1= new GlideDuration();

dur1.setValue(dur.getValue());

current.u_time_until_first_assigned=dur1.getDurationValue();

This isn't working. It isn't adding anything to u_time_until_first_assigned, which stays blank.

 

Can anyone see what I'm doing wrong?

8 REPLIES 8

I've updated to your script, but it's still not calculating anything into the u_time_until_first_assigned field? Is there something wrong with how I'm entering the script?time until first assigned.png

@JP-ODU - you are doing it wrongly

 

to get date fields you need to use  fd_data and dot walk till the you get your field

 

refer felow:

Screenshot 2025-09-08 at 15.12.35.png

Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

Ankur Bawiskar
Tera Patron

@JP-ODU 

update your script as this

var gdt1 = new GlideDateTime(current.sys_created_on.getDisplayValue());

var gdt2 = new GlideDateTime(gs.nowDateTime());

var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2

current.u_time_until_first_assigned.setDateNumericValue(dur.getNumericValue());

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@JP-ODU 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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