Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Duration time for Requests

bradschamerloh
Kilo Expert

Currently, OOB is not setup to populate the Duration fields in RITM's.   Anyone know how to set this up?

9 REPLIES 9

Mike Patel
Tera Sage

You can do Scheduled Script that run daily

 calendar_duration

var gr = new GlideRecord("task");
gr.addEncodedQuery('active=false^calendar_durationISEMPTY^sys_class_name=sc_request');
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.calDateDiff(gdt1, gdt2, false);
	gr.calendar_duration = dur;
	gr.setWorkflow(false); 
	gr.update();
}

 

Business duration

var gr = new GlideRecord("task");
gr.addEncodedQuery('active=false^business_durationISEMPTY^sys_class_name=sc_request');
gr.autoSysFields(false); // so that the records don't have system updates
gr.query();
while(gr.next()) {
	// Create schedule 
var gsBusiness =new GlideSchedule('xxxxxxxxxxxxxxxxxxxxx'); //xxxxx sys id 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();
}

 

How can I be confident that it'll set it from there when it won't set it from a business rule?

My script is different and works on my instance. I'm doing below (getGlideObject) instead of getDisplayValue

gr.business_duration = gsBusiness.duration(gr.sys_created_on.getGlideObject(), gr.closed_at.getGlideObject());

So, I did some more work on it and in my log, I have it showing that the value gets set, but when i look at the list of tasks that are closed the duration field is empty. Maybe something is clearing the value of that field. This is mysterious.

I tried running this script as a "Fix Script" since I only need it to run once (updates the Duration field of Tasks where State = closed complete)

 

Unfortunley, I'm not getting any results in the Duration field.

 

var gr = new GlideRecord("task");
gr.addEncodedQuery('active=false^calendar_durationISEMPTY^sys_class_name=sc_request');
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.calDateDiff(gdt1, gdt2, false);
gr.calendar_duration = dur;
gr.setWorkflow(false);
gr.update();
}