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

Thank you!   This is working great.   I still need to test with automation but I was able to go in to existing records and got them to update.   Do you have to update something on the form for it to "update"?



I ran the following scheduled job (On Demand) and it seems to have worked for those that had a missing Outage Duration but I can't tell if it updated the others that had a value:



var inc = new GlideRecord('incident');


inc.addQuery('category', 'Club Outage'); // This will query for all Club Outages category.


inc.addQuery('state', 7); //This will go after anything in the closed state.Also ran it for Resolved ('state', 6)


inc.query();



while (inc.next()) {


        inc.setWorkflow(false);


        inc.autoSysFields(false);


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


        inc.update();


}


Carl,



    For the business rule to trigger, yes you will have to update something. In case of scheduled jobs it will update all the records matching your condition. For testing purposes,you can add some print statements to see what are the values before and after the update by running this script in the background scripts



var inc = new GlideRecord('incident');


inc.addQuery('category', 'Club Outage'); // This will query for all Club Outages category.


inc.addQuery('state', 7); //This will go after anything in the closed state.Also ran it for Resolved ('state', 6)


inc.query();


while (inc.next()) {


gs.print("Before Update "+ inc.number+'   '+ inc.u_outageduration_duration);


        inc.setWorkflow(false);


        inc.autoSysFields(false);


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


        inc.update();


gs.print("After Update "+ inc.number+'   '+ inc.u_outageduration_duration);


}