function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DevelopersDevelopers 

How to calculate due date excluding weekends in apex controller?

Hi Friends,

I have some requirement to calculate the due date excluding weekends. But, this could be calculated in apex controllers to be used in adhoc report. This calculations requires in apex controller to get no.of business days to calculate the due date value. It may be 30 days, 60 days, 90 days or 180 days due date from the source date. For Ex; Record createdDate.addDays(30), generally gives post date value after 30 days from this created date. But, I need to get Date value as 30th Business day from the createdDate value which excludes weekends. 

I have gone through some urls which suggests to create a formula field on object level. But, in my case, i need to do this calculation in apex controller and need to display due date value on my custom vf page and no need to capture in any salesforce object. I also used some custom logic which is used to get no.of business days between two dates. But, that login throws me CPU time limit exceed error while doing calculation for bulk records.

Please help me how to do this calculation.

Thanks,
Phanikumar
Tejas KardileTejas Kardile
Hi,

Please see salesforce link which will help you in calculating weekday's and weekend formulas.
https://help.salesforce.com/HTViewSolution?id=000004526

Thanks
DevelopersDevelopers
Thanks for the response Mr. Teja. But, i don't want to insert the data into any object. I am using one wrapper class to display the results over the page. For this, i dont want to go to insert the records into the salesforce object.

Please help me.

Thanks,
Phanikumar
Tejas KardileTejas Kardile
Hi,

Please go through this blog and see if this help you.
http://sfdcbydev.blogspot.in/2016/01/working-day-calculation-apex.html

Thanks,