Advertisements
Home > CRM 2011, JScript > Fixing time zone issues while reading datetime fields using jscript in CRM 2011

Fixing time zone issues while reading datetime fields using jscript in CRM 2011

When you read datetime field values using OData or SOAP , you may not get exact datetime values if your CRM application is using by users from different time zones.

We can fix the problem by using the “timezonebias” field from current users “UserSettings“

timezonebias & timezonedaylightbias are system-calculated fields based on the time zone of current user

Follow below steps to fix the problem

  • Read the “timezonebias” &“timezonedaylightbias”  field by querying  “UserSettings“ by current user id
  • Get your datetime field and substract the “timezonebias & timezonedaylightbias” fields to get exact value

Below is the script for the above steps

  • Read Current UserSettings

function RetrieveUserSettings(callBackFunction) {

try {

var serverUrl = Xrm.Page.context.getServerUrl();

var oDataEndpointUrl = serverUrl + “/XRMServices/2011/OrganizationData.svc/”;

//get current user id from context

var UserID = Xrm.Page.context.getUserId();

var RetrieveUserSetting = new XMLHttpRequest();

RetrieveUserSetting.open(“GET”, oDataEndpointUrl + “/UserSettingsSet(guid'” + UserID + “‘)”, true);

RetrieveUserSetting.setRequestHeader(“Accept”, “application/json”);

RetrieveUserSetting.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);

RetrieveUserSetting.onreadystatechange = function () {

callBackFunction(this);

};

RetrieveUserSetting.send();

} catch (e) {

alert(“Error on RetrieveUserSettings method”);

}

}

  • Call the above function to get “timezonebias & timezonedaylightbias” fields

RetrieveUserSettings(RetrieveUserSettingCallBack);

  • Read the “timezonebias” & “timezonedaylightbias” field from Call Back function and substract it from your datatime field value

   function RetrieveUserSettingCallBack(retrievedUserSetting) {

try {

if (retrievedUserSetting.readyState == 4) {

if (retrievedUserSetting.status == 200) {

var retrievedUser = this.parent.JSON.parse(retrievedUserSetting.responseText).d;

// Assume you got datetime field to below object

var dateValue;

var actMinutes = dateValue.getMinutes();

alert(“Skewed datetime – ” + dateValue); 

if (userSettings.TimeZoneBias != null) { 
            dateValue.setMinutes(userSettings.TimeZoneBias * -1); 
        } 

if (userSettings.TimeZoneDaylightBias != null) { 
            dateValue.setMinutes(userSettings.TimeZoneDaylightBias * -1); 
        }
// Add the actual minutes

dateValue.setMinutes(actMinutes);

alert(“Exact datetime – ” + dateValue);

}

}

} catch (e) {

alert(e.Description);

}

}

Here is the article on handling the time zone differences  in Plug-ins

🙂

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: