- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 09:02 PM
Hi All,
I have a custom field 'u_Agent_duration'(duration type) and it's on the Interaction table.
I would like to calculate the difference between created datetime and current datetime. Then populate the Agent duration field with the duration difference
I wrote this below BR on interaction table on After, Insert and update checked.
Unfortunately, it is not working.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var creationTime = new GlideDateTime(current.sys_created_on);
var nowTime = new GlideDateTime();
var duration = GlideDateTime.subtract(creationTime, nowTime);
current.u_Agent_duration = duration; // give proper field name here
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:36 PM
Hi,
so after 30mins you want the duration to be set?
then you can add 30mins to your now time and then get duration
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var creationTime = new GlideDateTime(current.sys_created_on);
var nowTime = new GlideDateTime();
nowTime.addSeconds(1800); // add 30mins i.e. 1800 seconds
var duration = GlideDateTime.subtract(creationTime, nowTime);
current.u_Agent_duration = duration; // give proper field name here
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:30 PM
We don't have any close date field.
Just we need populate the Agent duration of the interaction record when (sys_created_on)created datetime is more than current datetime.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 10:56 PM
When you say sys_created_on is more than current date time, what do you mean?
Is it possible to have created date being greater than current time?
var currentDate = new GlideDateTime(gs.nowDateTime());
var openedAt = new GlideDateTime(current.getValue("sys_created_on"));
return new GlideDuration(GlideDateTime.subtract(openedAt, currentDate));
Aman Kumar