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 store the business hours since the incident was last updated?

nayanmule
Tera Expert

Hi All,

 

I want to store the business hours in some field since the incident was last updated. 

For example if incident was last updated 2 days ago, I want to add that time in business hours & store it in some field. 

So, here the field will store the value as (9x2 = 18 hours) . 

 

Any help will be appreciated.

 

Thanks 

Nayan Mule

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@nayanmule 

what's your business requirement here?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar  , if you check my post 

https://www.servicenow.com/community/now-platform-forum/flow-for-incident-states-based-on-the-last-u...

 

I want to run a flow to update incidents based on state conditions & last updated business time. So, I was thinking to add a field to calculate the business hours since last updated incident time. 

Thanks

@nayanmule 

you can store this using this logic

1) make before update BR

2) pick the previous updated time from sys_updated_on, get the current date/time

3) subtract both the date/time using schedule and store in that field

something like this

(function executeRule(current, previous /*null when async*/) {
    // Define the business hours schedule
    var schedule = new GlideSchedule();
    schedule.load('your_business_hours_schedule_sys_id'); // Replace with your business hours schedule sys_id

    // Calculate the time difference in seconds
    var timeDiffInSeconds = gs.dateDiff(previous.sys_updated_on, new GlideDateTime(), true);

    // Convert the time difference to business hours
    var businessHours = schedule.duration(timeDiffInSeconds) / 3600; // Convert seconds to hours

    // Store the business hours in the custom field
    current.u_business_hours_since_update = businessHours;
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar  , however this didn't work