function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

DateTime to format correctly based on User's timezone for triggers

Hello everyone. I'm stuck at an issue where I THINK i know what's going on but need clarification and guide to solution.


I have a trigger and WebService set up on salesforce which.

The Trigger is doing the following.

When ever new Web-to-lead comes in, it checks to see if the lead email already exists as well as if it is document download. If it's document download, it inserts new record into a custom object with following information:


  • DocID:Double
  • DOcTitle:Text
  • Download Date: DateTIme
  • DisplayDate: text 


The trigger will convert the into string using format (MM/dd/yyyy HH:MM:sss)


The WebService basically inserts new downloads from our website with same information.


Here is the issue.


When records are added through WebService, the datetime conversion to string automatically uses WebService login's timezone and correctly displays the time.


HOWEVER, trigger is simply converting to String values which comes out as formatted UTC time value.  


My theory is that trigger doesn't have user time zone set and thus doesn't know what to do.


Is there work around for allowing triggers to also convert time based on webservice login? 

Rajesh ShahRajesh Shah

You can directly use System.Now.format() without passing any parameters.


As per the Apex Language Reference, this function returns the DateTime as a String using the Local Time Zone of the user. If the TimeZone cannot be defined, GMT is used.