I support a vaccine management system in salesforce. After a vaccine has been administered for a contact, a future appointment is scheduled immediately for the 2nd dose.
There is a formula field on the Service Appointment object that offsets an hour to account for daylight savings time for 2nd doses.
Currently, we are manually updating the User time zone to accommodate for DST.
- Is there a way to automate this so we don't have to adjust the time zone manually?
Also, after the time zone has been adjusted, all existing appointments do not change and have to be manually updated to reflect the new time.
- Is there a way to automate this as well?
Here is the formula field on the Service Appointment object
IF(VALUE(LPAD(MID(TEXT( SchedStartTime -(5.00/24)), 12, 5),2)) = 12 ,
MID(TEXT( SchedStartTime -(5.00/24)), 12, 5)+' PM',
IF(VALUE(LPAD(MID(TEXT( SchedStartTime -(5.00/24)), 12, 5),2)) < 12, IF(VALUE(LPAD(MID(TEXT( SchedStartTime -(5.00/24)), 12, 5),2)) = 00, TEXT(12)+MID(TEXT( SchedStartTime -(5.00/24)), 14, 3),
IF(VALUE(LPAD(MID(TEXT( SchedStartTime -(5.00/24)), 12, 5),2)) < 10,
MID(TEXT( SchedStartTime -(5.00/24)), 13, 4), MID(TEXT( SchedStartTime -(5.00/24)), 12, 5)))+' AM', TEXT(VALUE(MID(TEXT( SchedStartTime -(5.00/24)), 12, 2))-12)+MID(TEXT( SchedStartTime -(5.00/24)), 14, 3)+' PM'))
Best Answer
As per the documentation:
(Emphasis is mine)
I therefore believe you need to remove the formula field and replace it by a regular Datetime field that is calculated in Apex (when creating the service appointment).
Note that it is important to calculate this field's value in the right way, otherwise you will simply hit the same sorts of problem again.
Basically you need to split the Datetime for the first injection into separate Date and Time values (since these then don't actually have a time zone), update the Date then recombine the values again into a Datetime. An example:
This ensures that the
secondInjection
date/time is calculated to use the same time-of-day as thefirstInjection
date/time and at a date offset from thefirstInjection
date, irrespective of DST transitions.If you need to target a time zone, in this calculation, that is not the same as the contextual user's one then you will most likely need to jump a lot more hoops to get this correctly functioning, applying offsets between the contextual user's time zone and the target time zone on the appropriate dates. Take a look at the TimeZone documentation for some more information and an example of using
TimeZone.getOffset
.