Calculating Business Days Via Groovy Script - Perficient Blogs
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

Calculating Business Days Via Groovy Script

Introduction

This blog shares the solution for calculating the total number of Working Days using groovy script.

Background

We had a requirement to calculate the next seven business days from the assigned Closed Date in Oracle Sales Cloud. Here, we investigate this functionality by using groovy calendar objects.

Technical Overview

First we have to define the global function. Here, two parameters are passing dCloseDate – the assigned closed Date and bsnsDays – the actual number of days (For our case, we are going to calculate the next seven business days. The closed date number should be excluded from 7 business days. So the value 6 will be pass from onChange event script ) .

Get the month, year and date of the Closed Date and convert into the proper date format as per the requirement. Then find the Day of the Closed Date using Calendar Object.

If Closed Date is on Sunday – Day of the week is 1

Closes Date is Monday – Days of the week is 2 vice versa.

So for Monday , Tuesday, Wednesday and Thursday we have one weekend for the period of 7 Business Days from Closed Date.

So now we have 6 Business days + 2 Days (1 weekend) =8 Days

If Sunday weekend is the Closed Date means, We should not count Sunday.

So now we have 6 Business days + 3 Days (1 weekend + Sunday Closed Date) =9 Days

For Friday and Saturday we have 2 weekends for the period of 7 Business days from Closed Date.

So now we have 6 Business days + 4 Days (2 weekend) =10 Days 

Validate and Save the global function. From OnChange Event call the global function by passing the value of two parameters like below:

 

Resulting Output:

This is the output from the above function in the log when executed.

Mar 26, 2018 04:29:28 AM UTC New Date : Tue Mar 13 04:29:28 UTC 2018
Mar 26, 2018 04:29:28 AM UTC days of the week: 3
Mar 26, 2018 04:29:28 AM UTC New Date : Wed Mar 21 04:29:28 UTC 2018

 

As you can see from above, the Closed Date is March 13 Tuesday and next 7 Working Days is March 21.Here we are including March 13 in the count of Working Days.

One thing to note is that the illustrative code above doesn’t take public holidays into consideration because I haven’t added the logic to do that.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up

Categories