17
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....
8
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 ...
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 ...
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).
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 ...
4
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 ...
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 ...
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
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 ...
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 ...
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.
...
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
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 ...
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 ...
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 ...
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" ...
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 ...
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 ...
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>>...
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 ...
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 ...
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 ...
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 ...
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 ...
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.
1
vote
Trigger Run Outside of Business Hours
If you've defined your business hours in metadata, you should be able to use the isWithin function to determine if the current datetime (DateTime.Now()) is within those defined business hours.
From ...
1
vote
Business Hours calculation is incorrect/ not consistent for certain dates
The timezone in BusinessHours has been assigned as EDT.
So, in your calculation, use BusinessHours.add() which returns the result Datetime in the local time zone.
Datetime deadline = BusinessHours....
1
vote
Does SFDX add any way of automating the setup of business hours?
As sfdcfox suggests, this stored as settings/BusinessHours.settings-meta.xml in an SFDX project does the trick for me (where I want 8 hours per weekday and zero hours on weekend days):
<?xml ...
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) ...
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)
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
businesshours × 85apex × 43
datetime × 15
holidays × 9
trigger × 8
formula × 8
unit-test × 7
date × 6
timezone × 6
formula-field × 4
case × 4
time × 4
soql × 2
deployment × 2
tasks × 2
cpulimit × 2
lightning-web-components × 1
javascript × 1
salesforcedx × 1
process-builder × 1
query × 1
reporting × 1
workflow × 1
email-template × 1
class × 1