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 calculate the On hold time?

Nihar2
Tera Contributor

Need to calculate the onhold time for a particular task. So I created a field 'On hold time' which contain the time when the ticket got onhold. So my plan is to get the difference of onhold time and current updated time, so I can populate the value on Total On-hold time.

 

So I wrote a before business rule with condition  State changes from On hold.

(function executeRule(current, previous /*null when async*/ ) {

var thold=current.u_total_on_hold_time;
if (previous.u_on_hold_time.getDisplayValue() != current.u_on_hold_time.getDisplayValue()) {
var onhold = new GlideDateTime(current.u_on_hold_time);
var time = new GlideDateTime(gs.nowDateTime());
var diff = GlideDateTime.subtract(onhold, time);
var date = diff.getRoundedDayPart();
var udate = thold+date;
current.u_total_on_hold_time = udate;
current.update();
}

 

})(current, previous);

 

These are the field details.

Nihar2_0-1675232628221.png

Please help me to do this.

1 REPLY 1

shaik riyaz1
Tera Contributor

Is the above code working ?