Salesforce: Adding specific working days to a Date

UPDATE: If you want to consider holidays and business hours you can check out this article.

Many time while coding we come across the requirement in which we need to calculate the EndDate from a StartDate after a specific number of business/working days. That means excluding the weekends (Saturday & Sunday) Following is the code snippet which does this. I know there can be an optimized way of doing this. But I didn’t want to spend more time on this.

Below method takes date and number of working days to be added to the date.

https://gist.github.com/prasannadeshpande/b56d3d78fb461fa7b27e5462e7543ac4

Method daysOff calculates the number of non-working days coming in the start date and end date. And after which we are again adding those number of days.


Posted

in

, ,

by

Tags:

Comments

6 responses to “Salesforce: Adding specific working days to a Date”

  1. Ivo Gonzaga da Silva Avatar

    Hi Prasanna . I’m veryt thanks for your colaboration in Salesforce Code.
    If you have material about the basic treene, please send for me
    SilvaG

  2. Simon Lawrence Avatar

    It should be noted this snippet does not take into account national/bank holidays, it just excludes weekends days. Otherwise, very nice.

  3. Chirag Avatar
    Chirag

    Hi

    After adding daysOff to the endDate what if the date comes out to be a weekend?
    You’ve not handled that situaution.

    1. Prasanna Avatar
      Prasanna

      Hi Chirag, Current code should be handling it. 🙂

  4. Salman Khoja Avatar

    Hi If I want to exclude Fri and Sat as weekends and modify this formula so that it calculates the number of working days instead of a date, what changes do I need to make to this formula?

  5. Avi Avatar
    Avi

    Hi can You provide it in the form of A Trigger

Leave a Reply

Your email address will not be published. Required fields are marked *