0

I have an afterupdate trigger that fires when a field called Product Start Time or Product End Time on a case is updated.

Please reference the code below for this example:

If the Product Start Time field's value is 9:00 PM and Actual Product Start Date field's value is 08/17/2023, I expect the fields Product_Start_EST__c, Product_Start_PST__c, Product_Start_GMT__c to respectively be 08/18/2023 12:00 AM, 08/17/2023 9:00 PM and 08/18/2023 4:00 AM This is however changing depending on whether the user who created the case is in CST or EST. How can I expect the above result regardless of user's (user who created the case) timezone? I'm fine with storing these converted date/time values as String values too, because the ultimate purpose is to plug in these into an email template.

Thanks

for(Case c:cases){
        if((Trigger.oldMap.get(c.Id).Product_Start_Time__c != Trigger.newMap.get(c.Id).Product_Start_Time__c) ||
        (Trigger.oldMap.get(c.Id).Product_End_Time__c != Trigger.newMap.get(c.id).Product_End_Time__c)) {

            Time startTime=c.Product_Start_Time__c;
            Time endTime=c.Product_End_Time__c;

            Datetime dtStart = Datetime.newInstance(c.Actual_Start_Date__c, startTime);
                      c.Product_Start_EST__c=dtStart.format('MM/dd/yyyy hh:mm a', 'America/New_York');
            c.Product_Start_PST__c=dtStart.format('MM/dd/yyyy hh:mm a', 'America/Los_Angeles');
            c.Product_Start_GMT__c=dtStart.format('MM/dd/yyyy hh:mm a', 'GMT');

            Datetime dtEnd = Datetime.newInstance(c.Product_End_Date__c, endTime);
            c.Product_End_EST__c=dtEnd.format('MM/dd/yyyy hh:mm a', 'America/New_York');
            c.Product_End_PST__c=dtEnd.format('MM/dd/yyyy hh:mm a', 'America/Los_Angeles');
            c.Product_End_GMT__c=dtEnd.format('MM/dd/yyyy hh:mm a', 'GMT');
            
            casesToUpdate.add(c);
    }

1 Answer 1

2

First off, I would store these as string fields, since DateTime fields always display in the current user's time zone.

Now, if you have a Date object and a Time object, and want a specific time zone, but can't predict the running user's time zone, you can use this utility method:

public DateTime convertTimeZone(DateTime dt, String fromTimeZone, String toTimeZone) {
        return dt.addSeconds(
            (
                TimeZone.getTimeZone(toTimeZone).getOffset(dt) - 
                TimeZone.getTimeZone(fromTimeZone).getOffset(dt)
            ) 
            / 1000
        );
    }

and then call it like this:

Datetime dtStart = Datetime.newInstance(c.Actual_Start_Date__c, startTime);
DateTime convertedDT = convertTimeZone(dtStart, 'America/Los_Angeles', UserInfo.getTimeZone().getID());

You are essentially saying that the current user did input the DateTime in 'America/Los_Angeles' time zone and we are gonna convert that into their time zone so that it will save correctly in the database.

So user is on Central time and they input 8/18/2023 9:00 PM. We expect that to be pacific time. We convert it to 8/18/2023 11:00 PM Central time. That way it saves to the database as 8/18/2023 9:00 Pacific time.

2
  • 1
    I would store the values as Date and Time separately so that formatting to the user locale is done automatically by Salesforce. Using strings is a bad idea since these will format in the contextual user's locale which can be different to that of the subsequent viewing user.
    – Phil W
    Aug 4, 2023 at 6:30
  • 1
    Also, you could easily get GMT from the Datetime to avoid having to do the initial conversion from user time zone.
    – Phil W
    Aug 4, 2023 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .