Issue with gs.calDateDiff function

agarwm
Kilo Contributor

Hi,

There is an out of box Business Rule on Incident Table named "mark_closed", which used gs.calDateDiff function to calculate difference between 'opened_at' and 'closed_at' fields.
However, this function is not working properly. There seem to be some issue. We have several incidents opened for which it is returning a value of '0'.
eg:
Opened : 2012-05-21 12:21:02
Closed : 2012-05-21 16:19:48
Duration : 00:00:00

This function is documented here:
http://wiki.servicenow.com/index.php?title=GlideSystem_Date_and_Time_Functions#calDateDiff.28String.2C_String.2C_boolean.29

I am unable to find out whats wrong with it. Can someone please help me in letting me know what am I missing here?

Thanks,
Mayank

7 REPLIES 7

CapaJC
ServiceNow Employee
ServiceNow Employee

One issue with calDateDiff is that it uses the first Calendar (sys_calendar record) it finds - there's no way to specify the calendar you want to use.

Is there a Calendar defined in the instance where that duration on that day would be zero?

If you want to use a Schedule to calculate the difference, vs. the default Calendar, you can try this method:
http://wiki.servicenow.com/index.php?title=Calculate_Duration_Given_a_Schedule

If there's an easier method I didn't find, hopefully someone else will chime in.


agarwm
Kilo Contributor

Hi CapaJC,

It seems you are absolutely correct. I found a calendar in our instance which was causing this issue.
However, it seems that the script in not selecting the calendar randomly. Because, when I deleted that calendar record, a warning is showing in logs: "getEventTarget() called with invalid record reference: sys_calendar.73ec28110a0a3c1b00fbaf67930861d8 for event: user.view, could have been deleted"

So it looks like that somehow the script is looking for that particular calendar only. However, I am unable to find any such link :(.

Thanks,
Mayank


CapaJC
ServiceNow Employee
ServiceNow Employee

That will often show in the logs after you delete something. Happens when the event gets processed, and tries to find the record that got delted. Doesn't necessarily mean it's associated with trying to use calDateDiff. Now that you have only (presumably) one Calendar, try your script again, and see if it works.


agarwm
Kilo Contributor

I tried running it again.
But its displaying the same warning message when I am running the script and now its returning 'null' instead of '0'.

Thanks,
Mayank