CRM TimeZone issue using JavaScript

Problem: When we try to save a datetime field from an HTML TextBox (date-picker)  to CRM using oData in JavaScript, the datetime is always saved incorrectly.

Why This happens:

  • In JavaScript, date object is always read in local timezone. But CRM always accepts date Object in UTC from any client application. Then it shows Date and Time in the TimeZone selected by the User for CRM environment.
  • So, when we send this data to CRM using oData, it assumes that the date is in UTC timezone and thus we see incorrect time in CRM views and forms for the record.
  • Also we cannot convert to required TimeZone in JavaScript. We can convert Date object to UTC using .ToUTC() method, but it returns a Date string instead of Date Object and we need Date object to pass to oData.
But Don’t you worry, we have found the resolution !! 🙂 

In our example, we were trying to create an appointment from a HTML Form using a custom datepicker for setting Start date of the Appointment record.

IMG_04122014_150943

Resolution:

1. Read the local date in a string in JavaScript like below.

// textboxvalue = ‘2015/04/15 00:20’

var scheduledStart = new Date(textboxvalue + ‘ UTC’);

2. This will get the UTC date for the date specified. I am running the code from IST. So, the Scheduled Start will be :

“Thu Jan 15 2015 05:50:00 GMT+0530 (India Standard Time)”

3. When we pass this data to oData create/ update method, we will now see the correct date as expected. Below is the code to create an appointment using oData with required date conversions to see correct data

function createAppointment(currentAccount) {
“use strict”;
var appointment = new Object();
var scheduledStart = new Date(currentAccount.scheduledStart + ‘ UTC’);
var scheduledEnd = new Date(currentAccount.scheduledStart + ‘ UTC’);
scheduledEnd.setMinutes(scheduledStart.getMinutes() + 30);
appointment.Subject = “Sales Visit – ” + currentAccount[“Name”];
appointment.RegardingObjectId = { Id: currentAccount[“Id”], LogicalName: currentAccount.EntityName.toLowerCase(), Name: currentAccount[“Name”] };
appointment.ScheduledStart = scheduledStart;
appointment.ScheduledEnd = scheduledEnd;
var jsonEntity = window.JSON.stringify(appointment);
var createRecordReq = new XMLHttpRequest();
var ODataPath = Xrm.Page.context.getServerUrl() + “/XRMServices/2011/OrganizationData.svc”;
createRecordReq.open(‘POST’, ODataPath + “/” + ‘AppointmentSet’, false);
createRecordReq.setRequestHeader(“Accept”, “application/json”);
createRecordReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
createRecordReq.send(jsonEntity);
JSON.parse(createRecordReq.responseText).d;
//Alert the Id of the created record

}

Hope this is helpful !!

The above resolution is successfully tested.. Please email/ comment if there is any other solution for this problem.

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 )

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