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 keep running total of hours for a ticket.

Elizabeth Barlo
Tera Contributor

I have created two fields on Incident table:

 

Time Spent (Value in minutes of time spent working on this item for current update)

Total Time Spent (Sum of all Time Spent entries on the ticket)

 

I need assistance with writing the business rule that updates the Total Time Spent field every time the Time Spent field is updated with a new value.  This will allow us to calculate the total time spent on the ticket so we can compare it with the originally estimated level of effort.

5 REPLIES 5

Elizabeth Barlo
Tera Contributor

 Hi, 

Below is what we ended up using to accomplish this:

(function executeRule(current, previous /*null when async*/) {
    if (current.u_time_spent.changes()) {
       
        var timeSpent = parseInt(current.u_time_spent, 10) || 0;
        var currentTotal = parseInt(current.u_total_time_spent, 10) || 0;
       
        current.u_total_time_spent = currentTotal + timeSpent;
       
        current.u_time_spent = '';
    }
})(current, previous);