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

ok so how is u_bc_time_entry related to incident, you may not be able to 'dot-walk' if it's not a field on incident, it's related another way, i.e. incident is the parent, then you will need to query for that record.



if they are seperate record on a table you would need to run GlideRecord query for those as well to add them to totalDur.



if you print out;


gs.addInfoMessage(current.u_bc_time_entry.u_hours_worked);


do you get anything, if no then this is not fetching that value because that dot-walk is not working.


jayarajantony
Giga Contributor

Hi G N,


You can use dateNumericValue() function which will convert duration back to seconds and will set it back to duration after adding another duration.



var dur = new DurationCalculator();


dur =gr.duration.dateNumericValue() + gr.duration.dateNumericValue();


gr.duration.setDateNumericValue(dur);




Hope this can help you!


This worked for me!   Thanks a ton!


Mary T
Mega Expert

If you have a while loop inside GlideRecord and I need to add the duration of each, how do you set the initial to 0 so you can add the first record?  I tried setting something like dur =gr.duration.dateNumericValue() and it would not work.