- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2018 05:14 PM
I have have a business rule to calculate the duration and business duration on the Catalog Task table. The duration calculates fine but the problem I found was that the business duration wasn't calculating correctly. I later found that my script was call the system calendar. I changed the system calendar to reflect our work hours and holidays correctly but my problem is how do I recalculate all the previous catalog task's business durations.
This is my business rule script
TABLE : Catalog Task [sc_task]
current.active = false;
if (current.closed_by.nil())
current.closed_by = gs.getUserID();
if (current.closed_at.nil()) {
current.closed_at = gs.nowDateTime();
current.business_duration = gs.calDateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),false);
current.business_stc = gs.calDateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),true);
current.calendar_duration = gs.dateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),false);
current.calendar_stc = gs.dateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),true);
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2018 07:20 PM
On your code you forgot to replace gr with current.
try below
current.active = false;
if (current.closed_by.nil())
current.closed_by = gs.getUserID();
if (current.closed_at.nil()) {
current.closed_at = gs.nowDateTime();
current.calendar_duration = gs.dateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),false);
current.calendar_stc = gs.dateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),true);
var gsBusiness = new GlideSchedule('c509a299db122300e020f8fdbf9619f8'); //sysid of schedule
// Get duration based on schedule
current.business_duration = gsBusiness.duration(current.opened_at.getGlideObject(), current.closed_at.getGlideObject());
current.business_stc = gsBusiness.duration(current.opened_at.getGlideObject(), current.closed_at.getGlideObject());
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2018 07:25 PM
you need below for business duration
var gsBusiness =new GlideSchedule('1524c5d40f0cb100465d46ace1050ed3'); //sysid of schedule
// Get duration based on schedule
current.business_duration = gsBusiness.duration(gr.opened_at.getGlideObject(), gr.closed_at.getGlideObject());
current.business_stc = gsBusiness.duration(gr.opened_at.getGlideObject(), gr.closed_at.getGlideObject());
you can run below in background script or once scheduled script. Add more fields as needed.
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());
gr.calendar_duration = gs.dateDiff(gdt1, gdt2, false);
var gsBusiness =new GlideSchedule('1524c5d40f0cb100465d46ace1050ed3'); //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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2018 06:44 PM
This is my new business rule but it is now not calculating anything for the Business Duration.
current.active = false;
if (current.closed_by.nil())
current.closed_by = gs.getUserID();
if (current.closed_at.nil()) {
current.closed_at = gs.nowDateTime();
current.calendar_duration = gs.dateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),false);
current.calendar_stc = gs.dateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),true);
var gsBusiness = new GlideSchedule('c509a299db122300e020f8fdbf9619f8'); //sysid of schedule
// Get duration based on schedule
current.business_duration = gsBusiness.duration(gr.opened_at.getGlideObject(), gr.closed_at.getGlideObject());
current.business_stc = gsBusiness.duration(gr.opened_at.getGlideObject(), gr.closed_at.getGlideObject());
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2018 07:01 PM
Is your business rule onbefore or after ? and also check if insert and update is checked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2018 07:08 PM