The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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

Bhuvan
Kilo Patron

@JP-ODU 

 

Use metric definitions that provides this information out of box or you can configure new definition and system will provide the details. There is no need to create it via Flow Designer.

 

Refer below articles,

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0751559

 

https://www.servicenow.com/community/performance-analytics-blog/how-to-create-a-historical-report-ba...

 

If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan

@JP-ODU 

 

Did you get a chance to review this ?

 

If my response helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

@JP-ODU 

 

Thanks for marking the post as helpful.

 

As per community guidelines, you can accept more than one answer as accepted solution. If my response helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Ajay_Chavan
Kilo Sage
Fix your script:

javascriptvar created = new GlideDateTime(current.sys_created_on);
var assigned = new GlideDateTime(current.u_first_assignment_change);
var duration = GlideDateTime.subtract(created, assigned);
current.u_time_until_first_assigned = duration.getDurationValue();

Issues in original:

Used gs.nowDateTime() instead of current.u_first_assignment_change
Wrong subtraction order (created - now = negative)
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*****