Count Duration Exclude Weekends and Public Holidays in Salesforce Without Any Codes
Currently, there is no default way for Salesforce to count Duration (days)excluding Weekends and Public Holidays in a field.
The Idea for this feature has been posted since 2012 in https://success.salesforce.com/ideaView?id=08730000000I75dAAC
Based on my google search research, currently there is a way to achieve this function using Apex Trigger as you can see here. But this can only configured if we have knowledge about Apex Trigger and not ideal way for an Admin.
In this article, I would like to share how I achieve this without Apex at all and only using Opportunity Object and default Holidays Configuration Object. The use case for this article is I want to count Opportunity Duration from Created Date to Close Date.
a. Add your local holidays in Setup (Setup -> Search for Holidays in Quick Search), Flag as recurring if the holiday occurs annually
b. Create a new custom number field In Opportunity for Holidays counter between the Created Date and Close Date I named this field Holidays Counter
c. Create an Autolaunched Flow (Setup-> Search Flow->New Flow)to access the Holidays Object and count the holiday days between Created Date and Close Date. You can follow the configuration from images below
d. Create a process builder as a a trigger in Opportunity Object (Setup-> Search Process Builder->New) You can follow the configuration from images below
e. Create a new number formula field to count the Duration excluding Weekends and Public Holidays
IF(
((CASE(MOD(CloseDate — DATE(1985,6,24),7),
0 , CASE( MOD(CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,3,3,4,4,5,5,5,6,5,1),
1 , CASE( MOD(CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,3,3,4,4,4,5,4,6,5,1),
2 , CASE( MOD(CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,3,3,3,4,3,5,4,6,5,1),
3 , CASE( MOD(CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,2,3,2,4,3,5,4,6,5,1),
4 , CASE( MOD(CloseDate — DATEVALUE(CreatedDate) ,7),1,1,2,1,3,2,4,3,5,4,6,5,1),
5 , CASE( MOD(CloseDate — DATEVALUE(CreatedDate) ,7),1,0,2,1,3,2,4,3,5,4,6,5,0),
6 , CASE( MOD(CloseDate — DATEVALUE(CreatedDate) ,7),1,1,2,2,3,3,4,4,5,5,6,5,0),
999)
+
(FLOOR((CloseDate — DATEVALUE(CreatedDate) )/7)*5))-1) = -1, 0,
((CASE(MOD(CloseDate — DATE(1985,6,24),7),
0 , CASE( MOD( CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,3,3,4,4,5,5,5,6,5,1),
1 , CASE( MOD( CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,3,3,4,4,4,5,4,6,5,1),
2 , CASE( MOD( CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,3,3,3,4,3,5,4,6,5,1),
3 , CASE( MOD( CloseDate — DATEVALUE(CreatedDate) ,7),1,2,2,2,3,2,4,3,5,4,6,5,1),
4 , CASE( MOD( CloseDate — DATEVALUE(CreatedDate) ,7),1,1,2,1,3,2,4,3,5,4,6,5,1),
5 , CASE( MOD( CloseDate — DATEVALUE(CreatedDate) ,7),1,0,2,1,3,2,4,3,5,4,6,5,0),
6 , CASE( MOD( CloseDate — DATEVALUE(CreatedDate) ,7),1,1,2,2,3,3,4,4,5,5,6,5,0),
999)
+
(FLOOR(( CloseDate — DATEVALUE(CreatedDate) )/7)*5))-1)
) — Holidays_Counter__c
Save the field
f. Check the result
If you want to check the validity of the result, you can go to https://www.timeanddate.com/date/workdays.html and set it based on your country, start date, and end date.
Enjoy, and comment if you need anything else!
Thanks