How to retrieve value from time_worked field?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2024 02:00 PM
Been having difficulty retrieving the value from the time_worked field, with the ultimate goal of summing several time_worked values from child records and then assigning the total to another time_worked field.
If I'm looping through child records via a Business Rule can anyone suggest the correct syntax to achieve this?
Current script is failing and pulling 1970 results:
// Query for child project tasks
var childTasks = new GlideRecord('pm_project_task');
childTasks.addQuery('parent', current.sys_id);
childTasks.query();
// Sum the time_worked field from all child tasks
while (childTasks.next())
{
totalTimeWorked += parseFloat(childTasks.time_worked);
}
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2024 11:39 AM
Hi @MBarrott,
I tested the following in my instance, I have two incidents with time worked values. Script logic follows
var inc = new GlideRecord('incident');
inc.addQuery('time_worked', "!=", "");
inc.query();
var totalSecs = 0;
while (inc.next()) {
// parse duration value
var twStr = inc.time_worked.toString();
gs.info("Number " + inc.number + ", time_worked = " + twStr);
var year = parseInt(twStr.substr(0,4));
var month = parseInt(twStr.substr(5,2));
var day = parseInt(twStr.substr(8,2));
var hours = parseInt(twStr.substr(11,2));
var mins = parseInt(twStr.substr(14,2));
var secs = parseInt(twStr.substr(17,2));
// gs.info("year = " + year + " month = " + month + " day = " + day + " hours = " + hours + " mins = " + mins + " secs = " + secs);
// caculate the number of seconds
var noYears = year - 1970; // hopefully, value will be less that one year
var noDaysecs = (day - 1) * (24*60*60);
var noMonthsecs = (month - 1) * 30 * noDaysecs;
var noHoursecs = hours * (60*60);
var noMinsecs = mins * 60;
totalSecs += (noMonthsecs + noDaysecs + noHoursecs + noMinsecs + secs);
gs.info('totalSecs = ' + totalSecs);
}
// convert to days, hours, minutes
var noDays = parseInt(totalSecs / (24*3600));
totalSecs = totalSecs % (24 * 3600);
var noHours = parseInt(totalSecs / 3600);
totalSecs %= 3600;
var noMins = parseInt(totalSecs / 60);
var noSecs = totalSecs % 60;
gs.info("noDays = " + noDays + ", noHours = " + noHours + ", noMins = " + noMins + ", noSecs = " + noSecs);
The results:
*** Script: Number INC0000001, time_worked = 1970-01-02 03:35:18
*** Script: totalSecs = 99318
*** Script: Number INC0000004, time_worked = 1970-01-01 12:20:10
*** Script: totalSecs = 143728
*** Script: noDays = 1, noHours = 15, noMins = 55, noSecs = 28
Maybe this will help you.