How to get duration for Task created to closed ?

yandp
Tera Guru

Hello all, 

I have a custom application, extended "task" table.

i need to get the task duration, like from record created to closed, taken how many day.

Refer to table below, there time data is all empty, how to make this available?

Thank you.

find_real_file.png

 

1 ACCEPTED SOLUTION

Sai Kumar B
Mega Sage
Mega Sage

@yandp 

You can use metric definition on your custom table, You can refer to the OOTB Incident  metric definition in the metric_definition table and modify the script accordingly

find_real_file.png

 

 

View solution in original post

5 REPLIES 5

Sai Kumar B
Mega Sage
Mega Sage

@yandp 

You can use metric definition on your custom table, You can refer to the OOTB Incident  metric definition in the metric_definition table and modify the script accordingly

find_real_file.png

 

 

@Sai Kumar , Hello, thank you for the sharing, i managed to get the duration.

Now trying to create report on this .

Is there a way to get business duration (excluded sat & sun) ?

Please share with me, appreciated.

Thank you. 

You have to use schedule to calculate duration between days excluding holidays and weekends

var dc = new DurationCalculator(); dc.setSchedule('eb65c2250fc76340e1f30dbce1050ee2', 'Los Angeles'); // Schedule sys_id and system time zone

var dur = dc.calcScheduleDuration(start, end); //start - your start date field and end - your end date field 

var business_days = dur/(60*60*9);

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

refer this OOB business rule and create same on your custom table

you would require Duration field type on your table

BR Name: mark_closed

Script: enhance as per your requirement.

setClosureFields();

function setClosureFields() {
	// incident_state is Closed so
	// 1. mark the task as inactive
	// 2. set the closed by to current user if not supplied
	// 3. set the closed time to now if not supplied
	current.active = false;
	if (current.closed_by.nil())
		current.closed_by = gs.getUserID();
	if (current.closed_at.nil())
		current.closed_at = gs.nowDateTime();
	
	// Update the fields that indicate the time/duration of the incident from open to close.
	// 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.opened_at.changes() || (current.closed_at.changes() && !current.isValidField("resolved_at"));
	var opened = current.opened_at.getDisplayValue();
	var closed = current.closed_at.getDisplayValue();
	
	if (dataChange || current.business_duration.nil())
		current.business_duration = gs.calDateDiff(opened, closed, false);
	
	if (dataChange || current.business_stc.nil())
		current.business_stc = gs.calDateDiff(opened, closed, true);
	
	if (dataChange || current.calendar_duration.nil())
		current.calendar_duration = gs.dateDiff(opened, closed, false);
	
	if (dataChange || current.calendar_stc.nil())
		current.calendar_stc = gs.dateDiff(opened, closed, true);
}

find_real_file.png

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader