16 votes

How can I get my correct current local time?

This code will get you the local time for the logged in user, by leveraging the timezone offset to subtract/add from GMT. It's even smart enough to account for Daylight Saving. TimeZone tz = UserInfo....
Sebastian Kessel's user avatar
7 votes

How can I get my correct current local time?

DateTime values are represented by an epoch time in GMT, and converted to the appropriate date/time when requested via the appropriate methods. System.debug, coincidentally, always displays GMT. Try ...
sfdcfox's user avatar
  • 491k
7 votes
Accepted

Calculating business days using BusinessHours class

It might be more clear to calculate the diff you want algebraically. If you want 10 hours per day, and you have an Integer that represents how many days you want to add, then you need to add: Long ...
Adrian Larson's user avatar
  • 150k
6 votes

BusinessHours class returns incorrect time difference

2.9 hours is 2 hours and 54 minutes. 0.1 hours is 6 minutes, so 0.9 hours is 54 minutes (9 * 6).
sfdcfox's user avatar
  • 491k
6 votes

How can I get my correct current local time?

DateTime usually shows the time in GMT. Format function retrieves the time based on user's timezone defined in Salesforce. Let's say, this timezone is CST. So, format() or formatLong() (Converts the ...
Santanu Boral's user avatar
5 votes
Accepted

Does SFDX add any way of automating the setup of business hours?

You can push/pull/deploy/retrieve BusinessHoursSettings as part of your source code repository. You'll find this file in /settings/BusinessHoursSettings.settings-meta.xml. If you're just getting ...
sfdcfox's user avatar
  • 491k
4 votes
Accepted

Add days to created date excluding weekend and holidays

You're doing more work than you have to in a lot of this code, but the core issue is your approach itself. You should create an Integer counter variable for the number of days elapsed, then increment ...
Adrian Larson's user avatar
  • 150k
4 votes
Accepted

Business Hours and Time Zones (EDT vs EST?)

Set your timezone to GMT -4:00 Eastern Daylight Time (America/New York). It will auto adjust for you as needed when set to that. If you set it to Standard Time as you have it will not adjust for DST
Eric's user avatar
  • 54.2k
3 votes

Business Hours difference calculation during BST

All data is always stored in GMT (now referred to as UTC) regardless of what the local time is. Changing your data to local time to work with it can be dangerous when it comes to Daylight Savings ...
crmprogdev's user avatar
3 votes
Accepted

Update action in x business hours

You could write a Case Trigger and call a trigger handler. Add this following method to return DateTime respecting business hours. public static DateTime SLADateTime(Datetime input, Long ...
Santanu Boral's user avatar
3 votes
Accepted

Apex Business Hours calculation glitch

You've hit upon a problem, all right, but not what you expected; you're using an Integer to try add milliseconds, which means you're limited to about 2 billion before you end up going negative. ...
sfdcfox's user avatar
  • 491k
3 votes
Accepted

Send email alert exactly after 4 business hours from Task Created Date

The issue will be resolved if you use BusinessHours.add(businessHoursId, startDate, intervalMilliseconds) which @Adrian has mentioned in comments. Find my analysis: I have configured my user's ...
Santanu Boral's user avatar
3 votes
Accepted

not being able to query BusinessHours in Case object

If you look at the documentation on Case, you will see that the actual field API Name is BusinessHoursId: SELECT BusinessHoursId FROM Case
Adrian Larson's user avatar
  • 150k
3 votes
Accepted

How are Business Hours added to a case?

Business hours gets set through Entitlement Process. Verify, if Entitlement Process has been defined at your org and Business hours will be associated with this Entitlement Process. Under this ...
Santanu Boral's user avatar
3 votes
Accepted

Long to Double Calculation Getting Rounded to Zero Incorrectly

When both sides of mathematical division are Integer or Long, the fractional part is discarded. At least one side must be a Decimal or Double before dividing. The easiest way to do this is to add .0 ...
sfdcfox's user avatar
  • 491k
3 votes
Accepted

How to check if a Date is within Business Hours for Multiple TImezones

The easiest solution, if you are able to use Apex for this checking, is to first convert the user's date/time value to the business hours timezone then apply the check. Perhaps something like (you ...
Phil W's user avatar
  • 36.2k
2 votes

Calculation of business hours between two date time fields

You most likely (from your comment) have not set up the business hours so they are all set to the default of 24 Hours a day 7 days a week. A quick google search for "Salesforce set up business hours" ...
Eric's user avatar
  • 54.2k
2 votes
Accepted

BusinessHours class returns incorrect time difference

The following logic works for me Long inputMillisecs = BusinessHours.diff(businessHoursId, dateTime1, dateTime2); Integer total_duration_in_seconds = (inputMillisecs/1000).intValue(); //duration in ...
Santanu Boral's user avatar
2 votes

Find the next several days time according to business hours

If you just want to consider the Weekends then you can do that using formula Calculate the number of days between two dates while excluding weekends or weekdays. But there is no standard function ...
Tushar Sharma's user avatar
2 votes

Businesshours.nextStartDate is returning value which is on a holiday

Alright, so found out the reason why this was happening. I had forgotten to associate the Holidays with the business hours. I had entered the holidays in the system (System>>Company Information>>...
kt10208's user avatar
  • 93
2 votes

Setting due dates for autogenerated cases

You need to first define Business Hours in this screen considering holidays too. To calculate and use this Business Hours, Create a separate class for this. public with sharing class ...
Santanu Boral's user avatar
2 votes

Calculating Business Hours via Trigger and Class

As per the BusinessHours documentation: All methods are static. So you should use: BusinessHours.diff(bh.id, create, Respond) instead of trying to use a method from the instance you queried, which ...
Renato Oliveira's user avatar
2 votes

Number Formula - Two Date/Time Field Difference Within Business Hours

Consider using Apex instead of formula. Here's why as of January 2021 (SFDC API Version 50.0) . (@ for edits): Be skeptical of using formula fields for datetime difference calculations because of a ...
Peter Noges's user avatar
  • 1,110
2 votes
Accepted

Mocking BusinessHours records returns System.MathException: null

BusinessHours.isWithin uses data from the BusinessHours record stored in the database. Since you mocked the ID, it's not a real record, so when it tries to figure out the holidays, you'll end up with ...
sfdcfox's user avatar
  • 491k
2 votes

Mocking BusinessHours records returns System.MathException: null

You could resolve the issue as pointed out by sfdcfox by creating a BusinessHoursService with an exposed isWithin method. Then your testmethod could inject a mock BusinessHoursService that returned a ...
cropredy's user avatar
  • 71.3k
2 votes
Accepted

Does Date.addDays() method skip the weekends based on the Org's Business Hours settings?

It does not interact with your Business Hours at all. It just adds days. From the documentation on the Date class: addDays(additionalDays) Adds the specified number of additional days to a Date.
Adrian Larson's user avatar
  • 150k
1 vote

Does BusinessHours diff() method work with standard CreatedDate field in Before Update Trigger?

Putting comments to the answer, since this approach has solved your purpose. You can store the createdDate value in the custom field or use formula field to get CreatedDate value (@Eduard's comments) ...
Santanu Boral's user avatar
1 vote

Calculate No of working days difference between two dates?

For excluding holidays, Configure Business Hours excluding holidays and calculate time difference as below public static Integer getBusinessHourTimeDifference(Datetime startDate, Datetime endDate) ...
Santanu Boral's user avatar
1 vote
Accepted

DateTime set to an hour earlier

Use Datetime.newInstanceGmt instead. Datetime instance = Datetime.newInstanceGmt(year, month, day, hour, minute, second);
Adrian Larson's user avatar
  • 150k
1 vote

Calculate hours based on Task priority

I would suggest to do it Before update since you'll save Dml operations. Something like: Before for(Task T: (List<Task>)Trigger.new) { if(T.Priority!=NULL && T.real_hours__c ==...
Alexander Aeons Torn's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible