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.

Start the Timer(time_worked) field when the state changes

poorva1
Giga Guru

Hi all,

 

I want to start the Time field field when the state changes to In progress and want to pause it when it changes to on hold.

How to achieve this? 

I have made glide.ui.timer.started  - false, to stop it from starting automatically. 

poorva1_0-1762486420299.png

 

Thanks,

Poorva

 

7 REPLIES 7

Brian Lancaster
Kilo Patron

Time work only runs when someone is actively looking at the form. It also only saves that data when the records is saved so I'm curious on why you want to stop and start time worked? 

Renat Akhmedov
Kilo Sage

Hi @poorva1

Good point from @Brian Lancaster , it's always good to start from "Why?", why you really need it, and if you can answer, it will also be helpful to me to suggest you right solution. 

In the meantime, if you "just need it", you can create the onBefore Business Rule, for the right table, condition that "State is changed", 

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

    // Start timer when state changes to In Progress
    if (current.state == '2' && previous.state != '2') { // assuming '2' = In Progress
        current.timer_start = true;
        gs.eventQueue('timer.start', current, current.sys_id, current.state);
    }

    // Pause timer when state changes to On Hold
    if (current.state == '3' && previous.state != '3') { // assuming '3' = On Hold
        current.timer_pause = true;
        gs.eventQueue('timer.pause', current, current.sys_id, current.state);
    }

})(current, previous);


I hope it helps you. If yes, please mark my comment as helpful. Thank you!

Best regards,
Renat Akhmedov

Thanks @Renat Akhmedov, I'll give this a try. 

Hi @poorva1

Please let me know about the result, and please don't forget to mark it as helpful if my answer helped you in a some way, 

Best regards,
Renat Akhmedov