0

We have a existing Salesforce webservice. This webservice is used by an external application to create a custom object records in Salesforce.

As part of the webservice API call, we have a datetime field which gets updated in Salesforce.

Salesforce requires the incoming date time in GMT format and then it converts to Integration user time zone in Salesforce. So the external application converts the Date time to GMT before sending to Salesforce. They are doing this conversion by adding 4 hrs to date time and send it to Salesforce.

The external application is currently in EDT, when they switch to EST after daylight savings period ends, then their current conversion to GMT will fail. (after day light saving ends; GMT= EST+5)

The external application is not very robust with date time functions and they have very limited capability and also they cannot send the time zone details when calling the Salesforce webservice.

Given these limitations of external application

  1. How to dynamically convert the date time to GMT when the external application clock switches from Daylight savings to standard time. (they do not send us the time zone ;they follow the standard clock switch of EDT to EST & vice versa)

Things that does not change on the external application are:

  1. They are always on Eastern time zone
  2. Exteral ApplicationClock automatically switch from EDT to EST and vice versa
  3. They will start sending the Date time 'AS-IS' based on the current time zone on the system.
  • You say in #3 they send the Datetime as is, but earlier in your pos that there is a conversion process that adds 4 hours. Which is it? – Adrian Larson Aug 15 '16 at 17:47
  • Apparently the way they are converting to GMT (by adding 4 hours) before sending to Salesforce will fail when they switch to EST, they have commented that logic and now we are looking for a solution which will handle the day light to standard time zone switch and vice versa. – user5352 Aug 15 '16 at 17:54
  • Can you add the format in which you receive this data? – Adrian Larson Aug 15 '16 at 17:56
  • They cannot add it dynamically based on their system's current time zone. The only way to do it, is to write a custom logic ; if the date falls between Mar-Oct( example) then assume the date format is in EDT, else date format should be in EST. – user5352 Aug 15 '16 at 18:02
0

Try using the Datetime.newInstanceGmt method. If the running user for the integration is in the Eastern Time Zone, then Salesforce will take care of DST for you.

Here's an example of how it works for such a user:

Time noon = Time.newInstance(12, 0, 0, 0);    

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 1, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 1, 1), noon));
// newInstanceGmt = 2016-01-01 12:00:00
// newInstance = 2016-01-01 17:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 2, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 2, 1), noon));
// newInstanceGmt = 2016-02-01 12:00:00
// newInstance = 2016-02-01 17:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 3, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 3, 1), noon));
// newInstanceGmt = 2016-03-01 12:00:00
// newInstance = 2016-03-01 17:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 4, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 4, 1), noon));
// newInstanceGmt = 2016-04-01 12:00:00
// newInstance = 2016-04-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 5, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 5, 1), noon));
// newInstanceGmt = 2016-05-01 12:00:00
// newInstance = 2016-05-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 6, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 6, 1), noon));
// newInstanceGmt = 2016-06-01 12:00:00
// newInstance = 2016-06-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 7, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 7, 1), noon));
// newInstanceGmt = 2016-07-01 12:00:00
// newInstance = 2016-07-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 8, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 8, 1), noon));
// newInstanceGmt = 2016-08-01 12:00:00
// newInstance = 2016-08-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 9, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 9, 1), noon));
// newInstanceGmt = 2016-09-01 12:00:00
// newInstance = 2016-09-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 10, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 10, 1), noon));
// newInstanceGmt = 2016-10-01 12:00:00
// newInstance = 2016-10-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 11, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 11, 1), noon));
// newInstanceGmt = 2016-11-01 12:00:00
// newInstance = 2016-11-01 16:00:00

system.debug(Datetime.newInstanceGmt(Date.newInstance(2016, 12, 1), noon));
system.debug(Datetime.newInstance(Date.newInstance(2016, 12, 1), noon));
// newInstanceGmt = 2016-12-01 12:00:00
// newInstance = 2016-12-01 17:00:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.