You need to sign in to do that
Don't have an account?
shobana Ganesan
Formula Field to exclude weekends and business holiday.
Hi Experts,
I have a bussiness need to exclude weekends and bussiness holiday for the report purpose, kindly help me out on this.
Requirement:
"Formual_Field__c = (Difference in days between the Current_Date__c and Custom_Status_Date__c excluding the weekend and holidays)"
Thanks in Advance,
Shobana.G
I have a bussiness need to exclude weekends and bussiness holiday for the report purpose, kindly help me out on this.
Requirement:
"Formual_Field__c = (Difference in days between the Current_Date__c and Custom_Status_Date__c excluding the weekend and holidays)"
Thanks in Advance,
Shobana.G
I trust you are doing very well.
It is an idea (https://success.salesforce.com/ideaView?id=08730000000I75dAAC) to exclude both holidays and weekends in Formula.
If you want to calculate the number of days between two dates while excluding weekends or weekdays, you can refer to below knowledge article.
https://help.salesforce.com/articleView?id=000004526&type=1
However, you can use this Apex code to exclude holidays and weekends in Formula.
https://sfdcdev.wordpress.com/2011/09/24/handling-holidays-in-salesforce-apex/
Also, please refer to below link which might help you further.
https://success.salesforce.com/answers?id=9063A0000019QHUQA2
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks and Regards,
Khan Anas
Hi Shobana,
In order to achieve this requirement you must go with Business hours and Holiday table in Salesforce SetUp >> Business Hours [ Place your company business hours here ] / SetUp >> Holiday [ Place your company holidays here ]
It'll automatically exclude the weekends.
By querying both of these tables in a Trigger /Class you get the accurate results rather a formula field.
Please Mark this as solved if it works.
Sathish P
However it'll not exculde the HOLIDAYS, Becuase each org has its own holidays based on region /country. Hense salesforce dont have knowledge about list of holidays.
CASE(MOD( StartDate__c - DATE(1985,6,24),7),
0 , CASE( MOD( EndDate__c - StartDate__c ,7),1,2,2,3,3,4,4,5,5,5,6,5,1),
1 , CASE( MOD( EndDate__c - StartDate__c ,7),1,2,2,3,3,4,4,4,5,4,6,5,1),
2 , CASE( MOD( EndDate__c - StartDate__c ,7),1,2,2,3,3,3,4,3,5,4,6,5,1),
3 , CASE( MOD( EndDate__c - StartDate__c ,7),1,2,2,2,3,2,4,3,5,4,6,5,1),
4 , CASE( MOD( EndDate__c - StartDate__c ,7),1,1,2,1,3,2,4,3,5,4,6,5,1),
5 , CASE( MOD( EndDate__c - StartDate__c ,7),1,0,2,1,3,2,4,3,5,4,6,5,0),
6 , CASE( MOD( EndDate__c - StartDate__c ,7),1,1,2,2,3,3,4,4,5,5,6,5,0),
999)
+
(FLOOR(( EndDate__c - StartDate__c )/7)*5)
Steps to create:
Create a formula field that returns a number.
Paste in one of the two formulas.
Replace StartDate__c and EndDate__c with your custom field values.
If using Date/Time fields: Replace with DATEVALUE(YourCustomDateTime__c) instead.
Notes:
Weekdays are defined as Mon-Fri and weekends as Sat & Sun
Holidays are not addressed
June 24, 1985 is a long distant Monday used as a reference point
The result will include BOTH the START and END dates.
Mon-Sun is counted as 5 Weekdays and 2 Weekend Days.
Mon-Fri is NOT (Fri subtract Mon) = 4 Elapsed Days.
Sat-Sun is NOT (Sun subtract Sat) = 1 Elapsed Day.
If you use another Formula field as the start or end dates you MAY hit a compilation limit.
Workaround - Use workflow rules to save the output of the formula fields into a regular date field.
Reference: Knowledge Article (https://help.salesforce.com/articleView?id=000004526&type=1)
Please mark it as solved if it answer your question.
Sathish P
I am using the same code but I am not able to execute that. I was getting an error message as "Error: Syntax error. Missing ')'". I am trying with almost same code. But I am not able to do that can you please help me in that.
If you want to achieve deducting business hours using a formula field, or using a formula in a workflow to update a field, use the formula provided below. This formula give the number of business hours rounded to the nearest hour, so you lose minute precision. However you can probably adjust the formula to give minutes as well. Src: https://resources.docs.salesforce.com/218/latest/en-us/sfdc/pdf/salesforce_useful_formula_fields.pdf
If you are comfortable writing an Apex class to return the business hours, you can very easily accomplish this. Just go to setup and search Business Hours. Enter your business hours and even holidays. Now, create an Apex class that calls the diff() method of the BusinessHours class to get the difference between two times while deducting your business hours. That easy.
The signature of the diff(Id, DateTime, DateTime) method is:
diff(businessHoursId, startDate, endDate)
where startDate and endDate are DateTime fields and the difference in milliseconds is returned in a Long.
Example of method that calculates business minutes between two DateTimes using the BusinessHours class and diff(Id, DateTIme, DateTime) method: Business Hours class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm#apex_System_BusinessHours_diff
0 , CASE( MOD(DATEVALUE(Approval_Request_Sent__c) - DATEVALUE(Assigned_to_CS__c ,7)),1,2,2,3,3,4,4,5,5,5,6,5,1),
1 , CASE( MOD(DATEVALUE(Approval_Request_Sent__c) - DATEVALUE(Assigned_to_CS__c ,7)),1,2,2,3,3,4,4,4,5,4,6,5,1),
2 , CASE( MOD(DATEVALUE(Approval_Request_Sent__c) - DATEVALUE(Assigned_to_CS__c ,7))),1,2,2,3,3,3,4,3,5,4,6,5,1),
3 , CASE( MOD(DATEVALUE(Approval_Request_Sent__c) - DATEVALUE(Assigned_to_CS__c ,7)),1,2,2,2,3,2,4,3,5,4,6,5,1),
4 , CASE( MOD(DATEVALUE(Approval_Request_Sent__c) - DATEVALUE(Assigned_to_CS__c ,7)),1,1,2,1,3,2,4,3,5,4,6,5,1),
5 , CASE( MOD(DATEVALUE(Approval_Request_Sent__c) - DATEVALUE(Assigned_to_CS__c ,7)),1,0,2,1,3,2,4,3,5,4,6,5,0),
6 , CASE( MOD(DATEVALUE(Approval_Request_Sent__c) - DATEVALUE(Assigned_to_CS__c ,7)),1,1,2,2,3,3,4,4,5,5,6,5,0),
999)
+
(FLOOR ((( DATEVALUE(Approval_Request_Sent__c) -DATEVALUE(Assigned_to_CS__c ))/7)*5)*24)