Catalog Task duration Business Rule

JR Guieb
Tera Expert

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?

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

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();
}

View solution in original post

3 REPLIES 3

Mike Patel
Tera Sage

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();
}

davida1
Giga Expert

Does anyone have code for calculating the Business Duration (business_duration) for closed SC Tasks?

shahida1
Mega Contributor

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);