Hi I tried subtracting Business hours. But somehow it is not working properly. Below is the code snippet that I used.
date mydate = date.parse('11/13/2012');
Datetime dt =datetime.newInstanceGMT(mydate.year(), mydate.month(), mydate.day());
List <BusinessHours> bhList = [SELECT Id FROM BusinessHours WHERE IsDefault = true] ;
Datetime newDt = BusinessHours.addGMT(bhList[0].id, dt, -(1*24* 60 * 60 * 1000));
System.debug('====='+dt+'====='+dt.format('EEEE')+'=>>>=='+newDt+'===='+newDt.format('EEEE'));
System.debug('====='+dt+'===='+(double)BusinessHours.diff(bhList[0].id, newDt,dt)/(1 * 24 * 60 * 60 * 1000L));
System.debug('========='+(double)BusinessHours.diff(bhList[0].id, datetime.newInstanceGMT(2012,11,13),datetime.newInstanceGMT(2012,11,12))/(1 * 24 * 60 * 60 * 1000L));`enter preformatted text here`
Few assumptions in Business Hours: All days other than sunday and saturday, working hours is 24 hours and for sunday and saturday No hours.
Problem : The above code is returning 2012-11-11 00:00:00(Sunday) for 2012-11-13 00:00:00(Tuesday) and business hours difference for this two date is showing .796
Debug log of the above code :
12:08:51.033 (33130000)|USER_DEBUG|[5]|DEBUG|=====2012-11-13 00:00:00=====Tuesday=>>>==2012-11-11 00:00:00====Sunday 12:08:51.033 (33443000)|USER_DEBUG|[6]|DEBUG|=====2012-11-13 00:00:00====0.7916666666666666 12:08:51.033 (33753000)|USER_DEBUG|[7]|DEBUG|=========-0.7916666666666666
Please help.
Attribution to: Avidev9
Possible Suggestion/Solution #1
Most of the BusinessHours Examples have the input startDate
in the local timezone rather than GMT.
It might be that the addGMT method is adjusting the dt
input as though it was local time.
Attribution to: Daniel Ballinger
Possible Suggestion/Solution #2
The difference is coming up as expected, if you add the following two lines of debugging:
System.debug(1*24* 60 * 60 * 1000);
System.debug(BusinessHours.diff(bhList[0].id, newDt,dt));
You will see that the diff is equal to the amount of time you're adding.
Attribution to: Scott VonSchilling
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4132