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

if they are seperate records you would need to scroll through them and add those totals up via a business rule similar to this:



var total = '';


var tc = new GlideRecord('u_time_card');


tc.addQuery('parent',current.sys_id);


tc.query();


while (tc.next()){


total = total + tc.duration;


}



current.u_total_duration = total;


Josh Cooper
ServiceNow Employee
ServiceNow Employee

This method concatenates the values, then when you set it, all the excess is dropped, so you end up with a value same as the first.


jwl178
Kilo Contributor

GN,



If you already are using the Start Time and End Time fields to calculate a Total Duration field I would suggest you not add the additional fields. This will add a lot of field duplication to your ticket if you are trying to keep the duration of each touch then using the Duration Calculator to get a grand total.



Can you please describe your environment a little more? Have you considered using the Time Worked field rather than multiple duration calculations?



Either way you might find these two pages helpful.


DurationCalculator - ServiceNow Wiki


Setting the Duration Field Value - ServiceNow Wiki


Thanks for your response, John.



This the line of code which i am using on Buisness Rule:-


function totalcaltime () {


  var totalDur ='';


  var inc = new GlideRecord('task');


  inc.addQuery('parent',current.sys_id);


  inc.query();


  if (count > 0) {


  while(inc.next()) {


  var dur = new DurationCalculator();


totalDur += current.time_entry.u_hours_worked.getGlideObject().getNumericValue();


  gs.addInfoMessage('Total Duration :' + totalDur);


  var tot = dur.calcDuration(totalDur / 1000);


  gs.addInfoMessage('Tot :' + tot);


  count ++;


  current.incident.u_total_time_worked.setDateNumericValue(tot);


  }


    }


}


Where u_hours_worked field is of Duration type and exist in time_entry table and containing the difference between Work Start Time & End time. which is getting calculated through BR and working fine. However, I have another field as Total Worked Hours and it is also of Duration type and placed in Incident table. Where i want addition of all u_hours_worked in time entry. While running this code its giving:-



Total Duration :undefined



Tot :false
Can you please let me know what needs to be modify?