{Knowhow} – How to get the server time in Dynamics CRM?

Not sure of the title explains the topic much. So let me illustrate with an example on what we were trying to achieve for our customer.

The customer had a requirement where the users would need to enter weekly data and the report would be locked for the week by Thursday 5 PM PST time of every week. Our servers were located in PST Time zone. If you really ask me, when I first got this requirement, I really didn’t pay any heed to this as I though this would be pretty simple stuff.

So let’s see how this small stuff gave me some sincere and serious thinking. Let decide on how to approach a feasible solution for this. Considering that our customers had this report being accessed by users all round the world, the following are the options we had.

  • Determine the PST time using getTimeZoneOffset method.
  • Using actions.

Let us evaluate each option in specific order

 

Determining the PST Time using getTimezoneOffset method:

  • In javascript, we have the getTmezoneOffset method. Using this method, if we know the time zone offset from GMT, we can determine the time of that particular time zone. So if it is PST time zone, it is (UTC – 8). So a simple code like below should give us the PST Time, based on the logged in user’s timezone.
    $date = new Date();

    // convert to msec
    // subtract local time zone offset
    // get UTC time in msec
    $utc = $date.getTime() – ($date.getTimezoneOffset() * 60000);
    $pstdate = new Date($utc + (3600000*’-8’));
All so fine this far. So what’s the problem if we use this. Well getTimezoneOffset does not take into effect the DST (Daylight saving’s time). So we had to write code to include the day light saving based on the month from which the daylight savings would start and then adjust the time accordingly. We discussed this with our customer and they did not want us to go that route.
  • So actions was our next big bet. We will create an action with output parameter of type datetime and then assign the output parameter to the ‘Process Execution Time’. All set we thought. But CRM still had few surprises up its sleeve.

When we called the action through javascript, we started getting the below error.

Conversion from type 'CrmDateTime' to type 'Date' is not valid
Oops. Surely did not expect this to happen. After bit of searching, realized that ProcessExecution time is of type CRM Datetime and cannot be set directly to a datetime variable. However if you set it to a datetime field of some entity, it would work. Strange isn’t it?
So now what? Finally I came up with the below idea.
  1. Create an action
  2. Set an output parameter of a the action of type ‘String’. Just notice I have mentioned String and not datetime. I will come back to this.
  3. Create a custom workflow activity with a output parameter of type string to hold the current server time.
  4. Set the output parameter in the custom workflow activity with the string representation of the server time

public class GetCurrentPSTTime : CodeActivity
{
[Output(“Current PST Time”)]
public OutArgument<String> CurrentPSTTime { get; set; }

        protected override void Execute(CodeActivityContext context)
{
CurrentPSTTime.Set(context, DateTime.Now.ToString(“yyyy-MM-dd hh:mm:ss”));
}
}

Just notice the format of the date that I have used here. The reason I have used it is because when I get back this in my client side code, I can directly convert it to datetime object in javascript.

The final step is to set the output parameter of the action with the value of the output parameter of the action

And finally I was able to achieve this. Seems like a mountain solution to a molehill requirement isn’t it.

 

Sharing this in case someone finds this useful.

 

Hope this helps!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s