Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to add two duration fields

manish123
Giga Guru

Hi All,

On Incident page, we have Time Card entry tab. And on that there is Duration field which is getting calculated from two other fields as Start Time and End Time. Therefore suppose a single incident should have multiple Duration added. Now on incident there is Total Duration field where i would like the addition of all these Duration for Time Card entries. Can anyone please let me know how can i do it?

33 REPLIES 33

marcguy
ServiceNow Employee
ServiceNow Employee

This is the out of the box business rule on task_time_worked table to update an incident's time_worked field, you should be able to utilise this:



it's also probably in your own instance:



https://xxxxxxxx.service-now.com/nav_to.do?uri=sys_script.do?sys_id=2ebd0e17c0a80a6d44cad01349d5d0f9




/**


Service-now.com



Description:


Allows for the modifications of task_time_worked values to update the task timer field.


Typically the timer is self controlled based on a cumulative value so changing a task time worked value will make the task timer appear inaccurate.


Requires setting property 'com.snc.time_worked.update_task_timer' to true


**/




updateTaskTimer();




function updateTaskTimer(){


      //get all time worked for this task


    var tw = new GlideAggregate("task_time_worked");


    tw.addQuery("task", current.task);


    tw.addAggregate("SUM", "time_worked");


    tw.groupBy("task");


    tw.query();


    if (tw.next()) {


  var timeworked = tw.getAggregate("SUM", "time_worked");




var inc = tw.task.getRefRecord();


inc.setValue("time_worked", timeworked);


//don't run task workflows or we'll end up with creating more time worked records


inc.setWorkflow(false);


inc.update();


gs.addInfoMessage(gs.getMessage("Time worked entry was modified, updated time in task") + " " + current.task.getDisplayValue());




    }




}


Can anyone please let me know how to convert Duration into seconds?


marcguy
ServiceNow Employee
ServiceNow Employee

Does this work?


http://wiki.servicenow.com/index.php?title=DurationCalculator#getSeconds.28.29


marcguy
ServiceNow Employee
ServiceNow Employee

According to the wiki it's stored in the DB as a number of milliseconds so if getSeconds doesn't work you should be able to just /1000



Duration Length of time. Stored in the database as an integer number of milliseconds, but displays in days, hours, minutes, and seconds.


Thanks, Mguy.


These are line of codes i am using :-


var strtdt = current.u_start_time.getDisplayValue();


var enddt = current.u_end_time.getDisplayValue();


var diffsec = gs.dateDiff(strtdt, enddt,true);


diffsec = parseFloat(diffsec)*1000;


current.u_timediff.setDateNumericValue(diffsec) // This one dispalying the substract calculation among start & end fields correctly in duration field.


However i have another duration field as Total Duration which will show the addition of all previous duration under that incident. And it is on same table time card. Suppose first entry for incident INC00001 is been made through time card TM00001 is 01 DAY 01 hours 00 MM 00 SS (on duration field it shown as 01 01 00 00 )and then second time card is enter with TM00002 and it has 00 DAY 00 hours 45MM 00 SS (on duration field it shown as 00 00 45 00) . Therefore, Total Duration will show the addition of these two time cards and it would be 01Day 01Hour 45MM 00SS)


Can anybody please let me know how can i get it?