web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Troubleshooting errors with Date time attribute in Web API call JSON payload

Arun Vinoth Profile Picture Arun Vinoth 11,615 Moderator
Today I was investigating these series of weird errors & able to sort all of them at the end of investigation. Actually I was creating/updating Dynamics CRM records in JavaScript/HTML web resource using Web API. The encounter is with a Date time field with user-local behavior. 

I was in impression that client context will take user timezone (Personal settings) into effect, but I am wrong and we have to always pass the UTC in JSON payload like 2019-7-23T12:19:39.522.

Error 1:
DateTime is less than minumum value supported by CrmDateTime. Actual value: 01/01/0001 00:00:00, Minimum value supported: 01/01/1753 00:00:00
Error 2:
An error occurred while validating input parameters: Microsoft.OData.ODataException: Cannot convert the literal '23.07.2019 8:30' to the expected type 'Edm.DateTimeOffset'. ---> System.FormatException: The string '23.07.2019 8:30' is not a valid AllXsd value.
Error 3:
The date-time format for <datefield> is invalid, or value is outside the supported range

Solution:
var currentdate = new Date();
var datetime = currentdate.getUTCFullYear() + "-"
+ (currentdate.getUTCMonth() + 1) + "-"
+ currentdate.getUTCDate() + "T"
+ currentdate.getUTCHours() + ":"
+ currentdate.getUTCMinutes() + ":"
+ currentdate.getUTCSeconds() + "."
+ currentdate.getUTCMilliseconds(); // + "Z";
var data = {
"new_name": "testing",
"new_publisheddate": datetime,
};
Xrm.WebApi.createRecord("new_entity", data).then(
function (response) {
},
function (error) {
});
Note: This was failing when the minute is lesser than 10 as the digit is expecting two digits. So small change in code like below will work nicely.

var currentdate = new Date();
var month = (currentdate.getUTCMonth() + 1) + "";
var date = currentdate.getUTCDate() + "";
var hour = currentdate.getUTCHours() + "";
var minute = currentdate.getUTCMinutes() + "";
var second = currentdate.getUTCSeconds() + "";
if (month.length < 2) month = '0' + month;
if (date.length < 2) date = '0' + date;
if (hour.length < 2) hour = '0' + hour;
if (minute.length < 2) minute = '0' + minute;
if (second.length < 2) second = '0' + second;
var datetime = currentdate.getUTCFullYear() + "-"
+ month + "-"
+ date + "T"
+ hour + ":"
+ minute + ":"
+ second + "."
+ currentdate.getUTCMilliseconds(); // + "Z";

UPDATE: I got a TIPS in my Twitter Inbox from Legend Guido to try toISOString() method for what I want, that was the perfect solution :)

Usage is like below:

new Date().toISOString();

Happy learning!


This was originally posted here.

Comments

*This post is locked for comments