Business Rule to calculate a duration field every time the record is updated

carlh
Kilo Guru

Hello,

We've created a Duration field on the incident form to help us track our outage durations.

I have 2 custom Date/Time fields

Outage Start Time

Outage End Time

and also a custom Outage_Duration field.

First we tried creating a client script on Submit and it was working but we have automated incident creation through a REST API that bypasses the UI Policies and client side scripts so we tried a "Before" Business Rule.   This was done so that it shouldn't matter how the incident is created or updated.  

Can anyone share a business rule that would calculate the Outage Duration field every time an Incident is updated with a category of "Club Outage" where the "Outage End Time is not empty"?

Here's the current Business Rule detail:

Table: Incident

Active = True

Advanced = True

When: Before

Order: 100

Insert and Update are TRUE

Filter Conditions:

Outage Start Time IS NOT EMPTY

Outage End Time IS NOT EMPTY

and

Category IS CLUB OUTAGE

Here's the script

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

current.u_outageduration_duration = gs.dateDiff(current.u_outage_starttime.getDisplayValue(), current.u_outage_endtime.getDisplayValue(), false);

})(current, previous);

Should this work every time the Incident is Inserted or Updated?

Please let me know if you see any obvious issues.

Thank you,

Carl Helber

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Carl,



  Everything looks good. Just change your script to this


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


var outage_dur= gs.dateDiff(current.u_outage_starttime.getDisplayValue(), current.u_outage_endtime.getDisplayValue(), false);


current.u_outageduration_duration.setDisplayValue(outage_dur);


})(current, previous);


View solution in original post

6 REPLIES 6

Abhinay Erra
Giga Sage

Carl,



  Everything looks good. Just change your script to this


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


var outage_dur= gs.dateDiff(current.u_outage_starttime.getDisplayValue(), current.u_outage_endtime.getDisplayValue(), false);


current.u_outageduration_duration.setDisplayValue(outage_dur);


})(current, previous);


The above suggestion works well if your're in the global scope.


However, if you're working within a scoped app, you'll need to use GlideDateTime like so:



var startDate = new GlideDateTime(current.u_outage_starttime.getDisplayValue());


var endDate = new GlideDateTime(current.u_outage_endtime.getDisplayValue());


var diff = GlideDateTime.subtract(startDate, endDate);



current.u_outageduration_duration = diff;


Hi,   I'm trying to do a similar script using sys fields.



I tried modifying the script you provided last year but was unable to get it working.



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


var initial_response = gs.dateDiff(current.sys_created_on.getDisplayValue(), current.sys_updated_on.getDisplayValue(), false);


current.u_initial_resp_glide_duration.setDisplayValue(initial_response);


})(current, previous);



Mind checking it out and answering this post for points?:


https://community.servicenow.com/message/1241746?et=watches.email.thread#1241746



Thanks for any help you can give.



Carl


Abhinay Erra
Giga Sage

Carl,



  Everything looks good and this rule will be triggered before an insert and update. Just change the script to this


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


var outage_dur= = gs.dateDiff(current.u_outage_starttime.getDisplayValue(), current.u_outage_endtime.getDisplayValue(), false);


current.u_outageduration_duration.setDisplayValue(outage_dur);


})(current, previous);