- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2018 07:22 PM
I have been searching on a way to get the duration field working for the catalog task and I have found a way to do so. I had to create a new business rule that mimics the "mark closed" business rule for the problem table.
After creating the business rule the duration field is now populating when a catalog task is closed. My issue is this works for any catalog task closed after I created the business rule. Does anyone know if there is a way to have this work for catalog tasks already closed prior to the new business rule?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2018 07:24 PM
run below code in background script or scheduled script.
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2018 07:24 PM
run below code in background script or scheduled script.
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2021 01:12 PM
Does anyone have code for calculating the Business Duration (business_duration) for closed SC Tasks?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2021 11:29 PM
Hi Davida
Please find BR for calculating Business Duration and Duration
Use Before Insert BR and state changes to closed complete and table as sc_task
(function executeRule(current, previous /*null when async*/ ) {
if (current.closed_at.nil())
current.closed_at = gs.nowDateTime();
// Update the fields that indicate the time and duration of this incident from open to resolve.
// Keep track of duration as a glide_duration value (dd hh:mm:ss) and as a pure number of seconds.
// Both calendar time and business time are maintained.
var dataChange = current.sys_created_on.changes() || current.closed_at.changes();
var opened = current.sys_created_on.getDisplayValue(); // created on
var resolved = current.closed_at.getDisplayValue(); // resolved date
if (dataChange || current.calendar_duration.nil()) {
// Schedule - 8-5 weekdays excluding holidays
// This will exclude weekends and all holidays
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); // sysid of schedule 8-5
var duration = schedule.duration(current.sys_created_on.getGlideObject(), current.closed_at.getGlideObject());
// Business Duration excluding holiday by taking schedule
current.business_duration.getGlideObject().setNumericValue(duration.getNumericValue());
// calender Duration
current.calendar_duration = gs.dateDiff(opened, resolved, false);
}
})(current, previous);