Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Script a Function Field to get duration from Now back to Pause Time

dgarod
Kilo Guru

I am new to JavaScript in SN, but I have a good developer's experience in other tools/languages. 

I am trying to use a Function field in London release to calculate the datediff between Now and pause_time in a Task_SLA table. According to documentation, and I have already done this, I can do something like this: glidefunction:datediff(resolved_at,sys_created_on), but these are regular fields on an Incident records. I need to use a Current date to subtract pause_time. Is it even possible to use a dynamic date?

I tried the below, but I am getting result of 0 sec

glidefunction:datediff(javascript:gs.nowDateTime(),sys_created_on)

glidefunction:datediff(gs.nowDateTime(),sys_created_on)

glidefunction:datediff(nowDateTime(),sys_created_on)

Please help!!!!!!

1 ACCEPTED SOLUTION

User658787
Giga Guru

datediff is different than gs.dateDiff.  The glidefunction version takes field names as inputs, not date/time values.  So you would need another field that stores the current date/time which I am assuming would be an issue for your use case if you are using a function field instead of some other options. 

View solution in original post

9 REPLIES 9

User658787
Giga Guru

datediff is different than gs.dateDiff.  The glidefunction version takes field names as inputs, not date/time values.  So you would need another field that stores the current date/time which I am assuming would be an issue for your use case if you are using a function field instead of some other options. 

I miraculously found that using "now()" as one of the inputs allows you to compare a date/time field with the current date time. The 'Type' field should be "Duration" and the 'Function definition' field would be this: 

glidefunction:datediff(now(),sys_created_on)

That's not documented anywhere (not surprising, unfortunately), but it is now! Make sure the "earlier" date/time is second in the input list, otherwise, you'll get an odd result. If you needed to display the duration since a certain field value, use what is above. If you need to display the duration to a certain field value, like Planned Start Date on a change request, use this: 

glidefunction:datediff(start_date,now())

Let me reiterate that this only produces good values if the first input is "later" than the second input. If your use case requires something else, I think you'd need to use GlideDBFunctionCaseBuilder and cross your fingers that you can figure it out.

This gives me an error saying "now is not a supported function , Script error in script." Did you have to update anything else to get it to work?                  

In another Community thread, I noted that this only works from the Dictionary view, not the reporting interface. Hopefully that's the issue you're experiencing. 

 

https://www.servicenow.com/community/developer-articles/function-fields-in-servicenow-reports-calcul...

dgarod
Kilo Guru

Apparently Function Fields have been broken in Madrid and being promised to be fixed in Orlando.