Need help with business duration calculation on a catalog task

JR Guieb
Tera Expert

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

 

1 ACCEPTED SOLUTION

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

View solution in original post

5 REPLIES 5

Mike Patel
Tera Sage

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

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

 

Is your business rule onbefore or after ? and also check if insert and update is checked.

it is set to on before update

 

find_real_file.png