How to Calculate Duration Field on Task table?

Laukik Udpikar
Tera Expert

How to Calculate Duration Field on Task table?

BR?

3 REPLIES 3

Ankush Jangle1
Kilo Guru

Hello,

There is OOTB Business rule or other server side script has been written to update this field value.

but you can use it according to your requirement

as you had mentioned you want to check the duration on task from open to close then you can write a business rule to update the duration value.

adding one thread here which will help you to populate the duration value .

https://community.servicenow.com/community?id=community_question&sys_id=660164bfdb7fd340a39a0b55ca96...

 

Please Mark it as Correct/Helpful if it Helps you

Brad Bowman
Kilo Patron
Kilo Patron

We're using this Business Rule, before Update, with the Condition

current.state.changes() && (current.state == 3 || current.state ==4 || current.state == 7)

and the Script

current.active = false;
current.calendar_duration = gs.dateDiff(current.opened_at.getDisplayValue(), gs.nowDateTime(), false);
current.calendar_stc = gs.dateDiff(current.opened_at.getDisplayValue(), gs.nowDateTime(), false);

This populates the duration, and Resolve Time, from open to closed.

Mike Patel
Tera Sage

If you want just duration than use below (ex of sc_task)

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

If you want Business duration than  use below

var gr = new GlideRecord('task');
gr.addEncodedQuery('active=false^calendar_durationISEMPTY');
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;
	var gsBusiness =new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //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();
}