How to Calculate Duration Field on Task table?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2020 03:24 AM
How to Calculate Duration Field on Task table?
BR?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2020 03:30 AM
Hello,
There is OOTB Business rule or other server side script has been written to update this field value.
but you can use it according to your requirement
as you had mentioned you want to check the duration on task from open to close then you can write a business rule to update the duration value.
adding one thread here which will help you to populate the duration value .
Please Mark it as Correct/Helpful if it Helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2020 03:32 AM
We're using this Business Rule, before Update, with the Condition
current.state.changes() && (current.state == 3 || current.state ==4 || current.state == 7)
and the Script
current.active = false;
current.calendar_duration = gs.dateDiff(current.opened_at.getDisplayValue(), gs.nowDateTime(), false);
current.calendar_stc = gs.dateDiff(current.opened_at.getDisplayValue(), gs.nowDateTime(), false);
This populates the duration, and Resolve Time, from open to closed.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2020 05:10 AM
If you want just duration than use below (ex of sc_task)
var gr = new GlideRecord("task");
gr.addEncodedQuery('active=false^calendar_durationISEMPTY^sys_class_name=sc_task');
gr.autoSysFields(false); // so that the records don't have system updates
gr.query();
while(gr.next()) {
var gdt1 = new GlideDateTime(gr.sys_created_on.getDisplayValue());
var gdt2 = new GlideDateTime(gr.closed_at.getDisplayValue());
var dur = gs.dateDiff(gdt1, gdt2, false);
gr.calendar_duration = dur;
gr.setWorkflow(false);
gr.update();
}
If you want Business duration than use below
var gr = new GlideRecord('task');
gr.addEncodedQuery('active=false^calendar_durationISEMPTY');
gr.autoSysFields(false); // so that the records don't have system updates
gr.query();
while(gr.next()) {
var gdt1 = new GlideDateTime(gr.sys_created_on.getDisplayValue());
var gdt2 = new GlideDateTime(gr.closed_at.getDisplayValue());
var dur = gs.dateDiff(gdt1, gdt2, false);
gr.calendar_duration = dur;
var gsBusiness =new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //sysid of schedule
// Get duration based on schedule
gr.business_duration = gsBusiness.duration(gr.sys_created_on.getGlideObject(), gr.closed_at.getGlideObject());
gr.setWorkflow(false);
gr.update();
}