XrmDefinitelyTyped icon indicating copy to clipboard operation
XrmDefinitelyTyped copied to clipboard

Updating datetime fields with XrmQuery throws error if field is set to date only

Open TomMalow opened this issue 5 years ago • 3 comments

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

  1. add a date only field to account
  2. Call xrmquery with the following code
XrmQuery.update(x => x.account, SOME_GUID, {
  new_dateonlyfield: new Date()
}); 
  1. 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'.
  1. Change XrmQuery to include date formatting
XrmQuery.update(x => x.account, SOME_GUID, {
  new_dateonlyfield: <any> new Date().toLocalDateString()
}); 
  1. 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

TomMalow avatar Mar 09 '20 10:03 TomMalow

This looks very much related to issue #156

mkholt avatar Mar 09 '20 10:03 mkholt

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

TomMalow avatar Mar 09 '20 10:03 TomMalow

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. :)

sixfourtwo avatar Sep 22 '22 10:09 sixfourtwo