0 votes
1 view
in Salesforce by (11.9k points)

We have a scheduled job that runs on the 1st of each month with a Preferred Start Time of 1am. The job was scheduled using the Salesforce interface (Develop | Apex Classes | Schedule Apex). When it runs, it sets a month field for records based on the System date (System.today();). Occasionally, the month is set wrongly, and I suspect it's due to the date variable set to the System date.

If I set the job to run at 1am, logged in as my User (with a time-zone set to CDT), using the interface, what value would be returned by System.today()? Would the current CDT date be returned, or the GMT date?

1 Answer

0 votes
by (31.6k points)

Scheduled jobs run as "system", but I think there's still a user context, which means Date.today() or  System.today() would be in CDT.

The docs say Date.today() returns the date in the current user's time zone. Based on the test below, the system knows who the user is, and it knows the user's time zone, so Date.today() would be the current date in the user's time zone. I confirmed this by setting my time zone to +10, and the system returned 2012-03-15 for the date.

// Brisbane +10 time zone

global void execute(SchedulableContext SC) {

  System.debug(DateTime.now()); // 2012-03-14 19:24:39

  System.debug(DateTime.now().formatLong()); // 3/15/2012 5:24:39 AM AEST

  System.debug(Date.today()); // 2012-03-15 00:00:00

  System.debug(UserInfo.getUserName()); // [email protected]

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...