- 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 09:10 PM
Hi,
BR should be before insert/update
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:06 PM
Thanks for your quick response Ankur!
Could you please help me to add condition, so that, BR should populate the custom field soon after the 30 minutes.
Or do we need a scheduled job to do this?
- 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:15 PM
Hey,
I think you can populate this information using calculate value in dictionary level:
var currentDate = new GlideDateTime(gs.nowDateTime());
var closedAt = new GlideDateTime(current.getValue("closed_at"));
var openedAt = new GlideDateTime(current.getValue("opened_at"));
var dur;
if (current.getValue("active") == true) {
dur = new GlideDuration(GlideDateTime.subtract(openedAt, currentDate));
} else {
dur = new GlideDuration(GlideDateTime.subtract(openedAt, closedAt));
}
return dur;
Aman Kumar