Dataverse Web API | JScript | EDM.Date conversion issue
While triggering ‘Create’ Action from jScript using the Web API we were getting following Edm.date conversion exception:
An error occurred while validating input parameters: Microsoft.OData.ODataException: Cannot convert the literal ‘2021-09-22T18:30:00’ to the expected type ‘Edm.Date’. —> System.FormatException: String ‘2021-09-22T18:30:00’ was not recognized as a valid Edm.Date.
Reason:
- In the Create Request payload, there was a Date value which was causing Edm.Date conversion issue.
- Following is the code snippet used to create a record for the custom table ‘raj_jobhistory’:
var entity = {};
entity.raj_companyname = "Microsoft";
entity.raj_startdate = Date.now();
entity["raj_employer@odata.bind"] = "/raj_employers(8D34D0F4-3F11-EC11-B6E6-000D3A3BA21F)";
Xrm.WebApi.online.createRecord("raj_jobhistory", entity).then(
function success(result) {
var newEntityId = result.id;
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
- In the above script, ‘raj_startdate’ is ‘Date only‘ field which was being set to ‘Date.now()’.
- For ‘Date only‘ field, Web API accepts the ‘yyyy-mm-dd‘ format. Since the date format was not being sent as yyyy-mm-dd application throwing the exception.
Fix:
- Use toISOString() function to change the date value format to yyyy-mm-dd.
- Following is the modified script.
var entity = {};
entity.raj_companyname = "Microsoft";
// Use toISOString() to change the Date format to yyyy-mm-dd
entity.raj_startdate = Date.now().toISOString().split('T')[0];
entity["raj_employer@odata.bind"] = "/raj_employers(8D34D0F4-3F11-EC11-B6E6-000D3A3BA21F)";
Xrm.WebApi.online.createRecord("raj_jobhistory", entity).then(
function success(result) {
var newEntityId = result.id;
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
Notes:
- EDM stands for Entity Data Model. OData service uses an abstract data model called (EDM) to describe the exposed data in the service.
- Refer this article on the usage of Dataverse Web API.
🙂
Categories: PowerApps
Comments (0)
Trackbacks (0)
Leave a comment
Trackback