You need to sign in to do that
Don't have an account?

Subtract business days(exclude weekends) from a given date
Hi all -
Using Apex, did you ever had to subtract the business days from any given date?
Like for ex: if I have to subtract from today 11/19/2014-10 days, it should exclude all the weekends and give me the final date 11/5/2014.
Please let me know if you have any function related to this requirement.
Thanks for looking into this.
Regards,
Priyanka
Using Apex, did you ever had to subtract the business days from any given date?
Like for ex: if I have to subtract from today 11/19/2014-10 days, it should exclude all the weekends and give me the final date 11/5/2014.
Please let me know if you have any function related to this requirement.
Thanks for looking into this.
Regards,
Priyanka
I have done similar to this before. please try the steps below.
1) Under Setup -> Company Profile-> Business Hours, create a new business hours which has no hours for Week ends.
2) Using Apex do the following
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name = '<your new Business hours>'];
Long duration = inputDays * -86400000;
BusinessHours.addGmt(bh.id, <Your_Date__c>, duration);
replace <your new Business hours> with your business hour name. also note that 86400000 is number of milling seconds per day and using negative operation as you want to subtract.
I looked at the business hours under setup and I see this
Default: Sunday24 HoursMonday24 HoursTuesday24 HoursWednesday24 HoursThursday24 HoursFriday24 HoursSaturday24 Hours
So do I have to create a new business hours which says Sunday 24 Hours, Saturday 24 Hours?
What is inputDays? is it the no of days that needs to be subtracted(in my above example(11/19/2014-10 days) --- inputDays is 10??
And <Your_Date__c> is the inputDate(in my above example(11/19/2014-10 days) -- <Your_Date__c> is 11/19/2014?
How does this work? I am confused.
Please let me know.
while creating new business hours, select the active check box, uncheck the 24 hours and clear the time in the Saturday and Sunday. once you save it will show as No Hours for Saturday and Sunday. all other days will show 24 hours
yes, you understanding is correct on the input days.
For <Your_Date__c>, you need to pass DateTime field. if you want to use current date, then use System.now()
BusinessHours.addGmt(bh.id, System.now(), duration);
I created new Business Hour. Its No Hours for Saturday and Sunday and the rest, 24 hours.
I am querying my bh by this:
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name = '<your new Business hours>'];
I have the duration as this: Long duration = 10 * -86400000;(considering inputDays = 10)
My date time is this(dt): 2014-12-27 05:00:00 ( a Saturday).
When I put a debug statement like this: system.debug('---business add gmt----'+BusinessHours.addGmt(bh.Id, dt, duration));
Its showing me in the logs as
19:48:54.088 (88938481)|USER_DEBUG|[116]|DEBUG|---business add gmt----2014-12-13 05:00:00
2014-12-27 - 10 days( excluding weekends) is 2014-12-15.. But BusinessHours.addGmt(bh.Id, dt, duration)); is showing as 2014-12-13 in my case?
Do you know why?
Thanks,
Priyanka
2) Also make sure that you dont pass the hours to the datetime just pass the date.
for example
BusinessHours.addGmt(bh.id, datetime.newInstance(2014, 12, 27), duration)
1) Pretty sure the calendar is saturday and sunday no hours. I even debug all the weekdays and weekend start and end time and it says saturday and sunday as null null and others with times.
2) I tried BusinessHours.addGmt(bh.id, datetime.newInstance(2014, 12, 27), duration) with no hours. still the same result
3) I tried BusinessHours.add(bh.id, datetime.newInstance(2014, 12, 27), duration) took out addGmt and instead tried add.. still the same result..:(
Date custDeliveryDate = Date.parse(Date);
Datetime dt = datetime.newInstanceGmt(custDeliveryDate.year(), custDeliveryDate.month(),custDeliveryDate.day());
system.debug('---dt----'+dt);
BusinessHours bh = [SELECT Id,SundayEndTime,SundayStartTime,SaturdayEndTime,SaturdayStartTime FROM BusinessHours WHERE Name = 'Schedule Email Business Hours'];
Long duration = 10 * -86400000;
system.debug('---duration----'+duration);
system.debug('---sunday---'+bh.SundayStartTime+bh.SundayEndTime);
system.debug('---saturday---'+bh.SaturdayStartTime+bh.SaturdayEndTime);
//BusinessHours.addGmt(bh.Id, dt, duration);
BusinessHours.addGmt(bh.id, datetime.newInstance(2014, 12, 27), duration);
system.debug('---business add gmt----'+BusinessHours.add(bh.id, datetime.newInstance(2014, 12, 27), duration));
Business Hours Name Schedule Email Business Hours
Time Zone (GMT-06:00) Central Standard Time (America/Chicago)
Business Hours
Sunday No Hours
Monday 24 Hours
Tuesday 24 Hours
Wednesday 24 Hours
Thursday 24 Hours
Friday 24 Hours
Saturday No Hours
Default Business Hours [Not Checked]
Active [Checked]
business add gmt----2014-12-15 06:00:00
1) execute the code you copied in the ananymous window and see the result
2) check what is the API version in your controller class.
Business Hours Name Schedule Email Business Hours
Time Zone (GMT-06:00) Central Standard Time (America/Chicago)
Business Hours
Sunday No Hours
Monday 24 Hours
Tuesday 24 Hours
Wednesday 24 Hours
Thursday 24 Hours
Friday 24 Hours
Saturday No Hours
Default Business Hours [Not Checked]
Active [Checked]
Tough luck. Shows the same as 13th dec and 15th dec
The set of holiday dates can be constructed from custom settings, the Holiday object or wherever.
You can also easily adjust for cultures where working days are not Mon - Fri
Cheers
Mike