How to create a Business Rule on a field to count days between two date fields.

Carrie Unga
Giga Contributor
  • We've created two date fields (MMDDYYY), a start and stop date that are manually entered on a form.
  • We've created a third date field to count the total days between the two dates.

PROBLEM: 

  1. The result is including weekends.
  2. If we move the start date out further than 7 days the result doesn't return correctly.
     EXAMPLE
     Sent to Network: 03-02-2020 
     Closed by Network: 03-17-2020
     Turn Around Time: Days 1 Hours 00 00 00 
     The correct turnaround time should be 10 days (between days only)

Using a Business Rule I've set the below script:

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

              //This business rule will calculate the number of business days between two fields: Sent to Network vs Closed by Network.

              current.u_pnm_network_turn_around_time = setDuration();

              function setDuration(){

                             var start = current.u_sent_to_network.getGlideObject();

                             var end = current.u_closed_by_network.getGlideObject();

                             var total = gs.dateDiff(start.getDisplayValueInternal(), end.getDisplayValueInternal(), false);

                             total = total.substring(0, 1);

                             total = total + ' 00:00:00';

                             //gs.info('Network Turn Around Time BR: ' + total);

                             return total;

 

              }

})(current, previous);

 

 

 

1 REPLY 1

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Your issue is this line:

total = total.substring(0, 1);

You are only grabbing the first character of the duration.  I would recommend the following instead:

total = total.toString().split(" ")[0];

This will split the DDDD hh:mm:ss by the space between.  Another option is to set your gs.dateDiff last paremater to true which will provide seconds instead and then you can calculate the number of days.