We ran across a bug (well, our clients did) a few months ago that gave us fits until last week. The solution was so simple, yet non-obvious that I wanted to write about it in the hopes no one else has to spend time working on it.
Dates Are Handled Differently in Custom Activities and Plugins
The work we were doing required us to pull data from a third party system twice. The first time came when a record was created, for which we used a Plugin. We also had to periodically refresh the data and for this we used a Custom Workflow Activity. The data from the third party consisted of about thirty fields, eight of which were dates.
The problem our clients found, though, was that sometimes some dates were off by about 5 hours. (Now, if you like to solve problems before the hero of the story, we were effectively at GMT-5 since we’re in the Central Time Zone, but it’s Daylight Savings Time.)
How We Figured it Out
Now, we’re smart enough to write code to account for GMT time and we faithfully put the code in both the plugin and the workflow. Then we ran the code and everything looked fine. For a time. It wasn’t until we ran the Plugin and then the Workflow back-to-back that we realized we, too, could produce this issue.
Date Handling in Plugins
It’s fairly well documented that plugins handle dates assuming all timestamps are GMT – 0.
However, when the data is displayed via CRM, it automatically adjusts the time to the user’s locale. Therefore, we were right to knock five hours off the incoming dates at creation time because CRM was going to add those five hours right back.
Date Handling in Custom Activities
We assumed what was true for plugins was also true for custom activities…and that was our mistake. The custom activity code, faithfully cut and paste from the plugin, took our dates
and stored them as is. No accounting for GMT time, no nothing. And suddenly, we had our five hour difference. The fix to the problem was simple. We remove the code to account for time differences and it worked.
There’s really nothing to be done about this issue, but if you suddenly find your times are off and you can’t figure out why, check your code. Make sure you’re accounting for GMT when you need to be (and that you aren’t when you shouldn’t).