How to retrieve value from time_worked field?

MBarrott
Mega Sage

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

HrishabhKumar
Kilo Sage

Hi @MBarrott ,

If this is your complete script, then the issue is you have not defined the 'totalTimeWorked' variable anywhere.

You have to define it before using it in the while loop, here is an updated script:

// 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
        var totalTimeWorked = 0;
        while (childTasks.next()) 
		{
            totalTimeWorked += parseFloat(childTasks.time_worked);
        }

 

Note: Make sure to use correct field names in the script.

 

Thanks,

Hope it helps.

If my solution turns useful, please mark it helpful and accept solution.

whitesmaverick
Giga Contributor

Hi MBarrott,

Thank you! I'll do that.

Also after you get that total you would have to add a line in to then fill in the special parent time worked field

Bert_c1
Kilo Patron

The 'time_worked' field defined on task is type: Timer, a GlideDuration object. that is why you see 1970. You will need to convert to seconds I believe to get anything meaningful.