Updating datetime fields with XrmQuery throws error if field is set to date only
Describe the bug
Setting a date only datetime-field through XrmQuery is not directly possible without formatting the date to a "YYYY-MM-DD" format and casting to any
To Reproduce
- add a date only field to account
- Call xrmquery with the following code
XrmQuery.update(x => x.account, SOME_GUID, {
new_dateonlyfield: new Date()
});
- Error is thrown
An error occurred while validating input parameters: Microsoft.OData.ODataException: Cannot convert the literal '(YYYY)-(DD)-(MM)T(HH):(mm):(ss).(ffff)Z' to the expected type 'Edm.Date'.
- Change XrmQuery to include date formatting
XrmQuery.update(x => x.account, SOME_GUID, {
new_dateonlyfield: <any> new Date().toLocalDateString()
});
- Update succeeds
Expected behavior I suggest it should be valid to set a date with a string like so
XrmQuery.update(x => x.account, SOME_GUID, {
new_dateonlyfield: new Date().toLocalDateString()
});
This means that a date field will have the signature new_dateonlyfield?: Date | String | null;
However, this does open it up for people updating dates with all kind of strange strings. But it is better than an any cast.
Environment
- CRM/D365/CDS version: 9.1.0.14134
- Tool version: 5.1.5
This looks very much related to issue #156
I also looked if these were related but am not that sure. The issue in #156 is about fetching date fields and this about setting date fields. Date and time is generally always a hassel :P
I'm getting a new error trying to update a date-only field:
"error": {
"code": "0x80040265"
"message": "EntityState must be set to null, Created (for Create message) or Changed (for Update message). EntityState of primaryEntity: Unchanged, RequestName: Update"
}
The <any> workaround doesn't seem to work anymore. At least not for me. :)