Ok so I've been trying to sort this out all day and I've not made a single bit of progress.
I have a set up a site on Salesforce as a web service endpoint. A client's system sends an HTTP request with a few bits of information as GET parameters and then our salesforce updates a record.
I noticed that the date was saving an hour later than it was and I cannot get it to save correctly no matter what I have tried.
The Custom_Object__c has two datetime fields, one which is set to Datetime.now() when the record is created, we'll call that Datetime_1__c. This one saves correctly. The other field, Datetime_2__c is set when the HTTP request comes through with a date string in the GET parameters. The date string is +01:00 time zone, and requires a bit of reformatting before converting.
The apex code is below:
String dtStr = g_datetime.substring(0, 19).replaceAll('T', ' ') + '+01:00'; System.debug('Date/Time string to save:' + dtStr); Datetime dt = Datetime.valueOf(dtStr); System.debug('Date/Time to save:' + String.valueOf(dt)); relatedRecord.Receipt_Received_At__c = dt;
The debug log shows these two entries:
13:58:07.038 (38233077)|USER_DEBUG||DEBUG|Date/Time string to save:2014-06-19 14:44:00+01:00 13:58:07.038 (38416001)|USER_DEBUG||DEBUG|Date/Time to save:2014-06-19 14:44:00
But when I go on the record detail page, the time shows as 15:44. My user time zone is BST (+01:00) and so is the company default, but when I set these to any other time zones it STILL puts the date an hour ahead.
What do I need to add or remove from my code to make this appear correctly?
The DateTime.valueOf(string) method has the following Usage note in the docs:
It is ignoring the +1 UTC offset.
If I run the following as anonymous Apex:
In this particular instance my Salesforce users timezone is set to (GMT-07:00) Pacific Daylight Time (America/Tijuana).
As an alternative, try formatting the input string in the IS0 8601 format. You can then convert it to a UTC DateTime with:
You can encode the timezone offset in the input date time using:
If you can, transfer the value in UTC. It will make your task easier. I.e. what happens if/when BST changes due to daylight savings time.