Annual Leave Entitlement Proration Rounding Up to multiple of 0.5 in SF Time Off scenario

Business Scenario:  Recently I was implementing time off solution for my client.  While implementing Annual Leave Entitlement for my clients, I have encountered a little complex scenario that I would like to share with you today. Also, discuss on some specific business rule functionality.

Annual Leave entitlement is pretty straight forward to configure and well-articulated in the implementation guide. My focus today is to explain a typical Proration logic for Termination or Hire Annual Leave Proration Rule.

Annual Leave Entitlement to be retrieved (from a lookup table) based on years with company and Position. Proration logic is to round up the prorated leave balance to multiple of 0.5 for Hire and Termination Case.

Example Scenario:

For Hire Scenario, if Yearly Entitlement is 18 days leave, below proration logic to be implemented:

A calculated prorated leave entitlement of 12.75 to 13.24 to be rounded to 13

A calculated prorated leave entitlement of 13.25 to 13.74 to be rounded to 13.5

A calculated prorated leave entitlement of 13.75 to 14.24 to be rounded to 14


Here is the rule with simple rounding logic and optimized database hitting:


Variable Deceleration


Rule Body


Trick I did is to perform the proration entitlement before actual rule execution, assigning it to a variable. Then apply the round rule on that variable.

Note 1: A variable used in any rule, can’t be deleted to modified, if that is used in actual rule par. So, to update a variable, one needs to remove it from the rule body part fiest.

Note 2: If a variable declared in any rule, but not used in the rule body, the variable will not get called at the run time

So, further frilling down on the var_calculatedamount assignment:

  1. Calculate the number of days an terminated employee will be eligible for leave calculation on the last day of work

Get Number Of Calendar Days()

                           Start Date: Accrual Rule Parameters.Accruable Start Date

                           End Date:  Accrual Rule Parameters.Accruable End Date

  1. Get the number of days in the year calculation is happening. This function is used to take into account the extra day on Leap year

Get Number Of Days For Year Of Date()

                            Date: Accrual Rule Parameters.Accruable End Date


  1. Divide Days obtained from point 1 with Days obtained from point 2, to get the fraction
  2. Fetch the yearly leave day’s entitlement from “Your Lookup Table”, based on employee’s years of experience and Position Title (Consultant, Lead Consultant, Principal Consultant etc.)
  3. Multiply Yearly entitlement obtained from Point 4 with Fraction from Point 3 to get prorated entitlement for the year
  4. Round off the prorated data obtained from point 5 to 2 decimal places (Precision 2) and with threshold (0.5)

Now, we get the prorated value till 2 decimal. Example if the yearly entitlement is of 18 days for the whole year, prorated entitlement may be like 12.25, 13.01 or 12.06.

Now we shall use simple Modulo function to achieve desired rounded value. That is prorated leave balance in the range 12.75 to 13.24 to be rounded to 13 and so on.

Modulo function on a decimal number returns only the decimal part of it with divisor is 1.

For example MODULO ( Number = 12.35 Divisor = 1 ), will return value 0.35.

Then simply compare the decimal part against the rage and post the desired leave like below:

If Modulo ( Number =  12.35 Division = 1 ) less than 0.25

Then Post Leave  ( Number = 12.35 – Module ( Number  = 12.35 ) ) = 12.35 – 0.35 = 12

Else if  Modulo ( Number =  12.35 Division = 1 ) Greater  than 0.24 and Less Than 0.75

Then Post Leave  ( Number = 12.35 – Module ( Number  = 12.35 )  + .5  ) = 12.35 – 0.35 + .5 = 12.5

Else Post Leave  ( Number = 12.35 – Module ( Number  = 12.35 )  + 1  ) = 12.35 – 0.35 + .5 = 13

In the above example final prorated rounded value will be 12.5

Thank You!

Thank you for reading this blog. Hope you find this interesting.  Request you to please share your comment and / or queries.

You can ask your query in community forum mentioned in the link below as well.