How to do the sum of pause time for all tasks in a RITM ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2023 05:13 AM
Hello All,
I need to have the sum of pause time for all tasks of a RITM.
For the moment, I have this run script below on my workflow which permit me to retrieve the pending time for the first task.
I have several tasks and I don't know how to script the rest so that the loop continues until the end of the tasks.
Could you help me please?
var pauseDuration;
var task_sys_id;
var duration_result;
var get_tasks_sysid = new GlideRecord('sc_task');
get_tasks_sysid.addQuery('request_item', current.sys_id.toString());
get_tasks_sysid.query();
while (get_tasks_sysid.next()) {
task_sys_id = get_tasks_sysid.sys_id.toString();
var slaCheck = new GlideRecord('task_sla');
slaCheck.addQuery('task', task_sys_id);
slaCheck.query();
while (slaCheck.next()) {
pauseDuration = (new GlideDateTime(slaCheck.pause_duration.toString()).getNumericValue()) / 1000;
duration_result = parseInt((pauseDuration), 10);
}
var hours = Math.floor(duration_result / 3600);
var minutes = Math.floor((duration_result - (hours * 3600)) / 60);
var seconds = duration_result - (hours * 3600) - (minutes * 60);
// Display formating for hours
if (hours < 10) {
if (hours == 0) {
hours = "00";
} else {
hours = "0" + hours;
}
}
// Display formating for minutes
if (minutes < 10) {
if (minutes == 0) {
minutes = "00";
} else {
minutes = "0" + minutes;
}
}
// Display formating for seconds
if (seconds < 10) {
if (seconds == 0) {
seconds = "00";
} else {
seconds = "0" + seconds;
}
}
}
current.variables.toto = hours + ':' + minutes + ':' + seconds;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2023 06:47 AM
SOLUTION
// Variables declaration
var task_sys_id = '';
var duration_string = '';
var duration_result = 0;
// Retrieve all tasks with request item sys_id = request item sys_id passed as parameter
var gr_get_tasks_sysid = new GlideRecord('sc_task');
gr_get_tasks_sysid.addQuery('request_item', current.sys_id.toString());
gr_get_tasks_sysid.query();
while (gr_get_tasks_sysid.next()) {
task_sys_id = gr_get_tasks_sysid.sys_id.toString();
// Retrieve SLAs for tasks
var gr_get_taskSlas_sysid = new GlideRecord('task_sla');
gr_get_taskSlas_sysid.addQuery('task', task_sys_id);
gr_get_taskSlas_sysid.query();
if (gr_get_taskSlas_sysid.next()) {
duration_string = (new GlideDateTime(gr_get_taskSlas_sysid.pause_duration.toString()).getNumericValue()) / 1000;
duration_result += parseInt(duration_string, 10);
}
}
var hours = Math.floor(duration_result / 3600);
var minutes = Math.floor((duration_result - (hours * 3600)) / 60);
var seconds = duration_result - (hours * 3600) - (minutes * 60);
// Display formating for hours
if (hours < 10) {
if (hours == 0) {
hours = "00";
} else {
hours = "0" + hours;
}
}
// Display formating for minutes
if (minutes < 10) {
if (minutes == 0) {
minutes = "00";
} else {
minutes = "0" + minutes;
}
}
// Display formating for seconds
if (seconds < 10) {
if (seconds == 0) {
seconds = "00";
} else {
seconds = "0" + seconds;
}
}
current.variables.toto = hours + ':' + minutes + ':' + seconds;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2023 07:04 AM
Hi
I would recommend creating an sla definition for the tasks that you want to measure. It will collect business elapsed time and actual elapsed time based on a schedule. Also the task_sla table has a handy field called pause duration.
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2023 07:26 AM
Hi,
Yes I have put a task duration on each task in order to have a SLA.
Thank you.
BR

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2023 07:42 AM
Hello @SBT1 ,
Please try below code if it works for you, I made some modifications.
var totalPauseDuration = 0;
var getTasksSysID = new GlideRecord('sc_task');
getTasksSysID.addQuery('request_item', current.sys_id.toString());
getTasksSysID.query();
while (getTasksSysID.next()) {
var taskSysID = getTasksSysID.sys_id.toString();
var slaCheck = new GlideRecord('task_sla');
slaCheck.addQuery('task', taskSysID);
slaCheck.query();
while (slaCheck.next()) {
var pauseDuration = 0; // Default to 0
if (!(slaCheck.pause_duration.nil()) || !(slaCheck.pause_duration=='NaN)) {
pauseDuration = new GlideDateTime(slaCheck.pause_duration.toString()).getNumericValue() / 1000;
}
totalPauseDuration += pauseDuration;
}
}
var totalSeconds = Math.floor(totalPauseDuration);
var hours = Math.floor(totalSeconds / 3600);
var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
// Format hours, minutes, and seconds
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
current.variables.toto = hours + ':' + minutes + ':' + seconds;
- Added totalPauseDuration variable to accumulate the pause durations for all tasks.
- Inside the inner while (slaCheck.next()) loop, calculate the pause duration for each task and add it to totalPauseDuration.
- After processing all tasks, convert totalPauseDuration to hours, minutes, and seconds and format them accordingly.
- Finally, set the formatted time as the value of the current.variables.toto field.
Kindly mark correct and helpful if applicable