When is a string a date?

Did you know that Dataverse will check if a string field is a date and try to truncate it before it saves it? Neither did I until today.

String fields are dates?

When updating an environment variable storing a datetime in round-trip format, I noticed it was truncated!

Testing further with a single table model-driven app containing all three types of string ‘field’ revealed that strings that look like dates are indeed truncated. Below is a screenshot of a table containing Text, Text Area and Multiline text fields.

Before Saving

Before Save

After Saving

After Save

Non-zero fractions

Non-Zero fractions

Via WebApi?

Is this something that the unified interface does? We can test in the browser using Xrm.WebApi.updateRecord as shown below.

await Xrm.WebApi.updateRecord("pgc_brand", "8377c102-eae4-eb11-bacb-000d3a9a7a8e", { "pgc_testtext": "  2012-07-14T00:00:00.0000Z" });

This still results in a truncated string being stored.

Checking in fiddler proves that the precision is sent over the network. We now know it’s not the Unified Intefrace or Xrm.Webapi library performing the truncation. It occurs on any WebApi request.

Webapi patch 1

Webapi patch 2

Via the Organisation Service

Using the excellent Code Now plugin for XrmToolbox by Alex Shlega we can quickly use IOrganisationService.Update to test:

OrgServiceUpdate1

We can confirm that the precision is sent over the wire with fiddler and it is.

OrgServiceUpdate2

In this instance the string is saved unchanged.

OrgServiceUpdate3

Via a Canvas Apps

Using the default three-pane canvas app we can view via fiddler that Canvas Apps are using a PATCH via the WebApi, and due to this the string trunction occcurs.

When does it (not) occur?

Adding any whitespace before or after date, or any other text will not truncate the string. The string will be reduced if it can be without losing precision. e.g.

   "2012-07-14T00:00:00.0000Z"    is truncated to   "2012-07-14T00:00:00Z"
   "2012-07-14T00:00:00.0050Z"    is truncated to   "2012-07-14T00:00:00.005Z"

These are not truncated

   " 2012-07-14T00:00:00.0000Z"
   "2012-07-14T00:00:00.0000Z "
   "abc 2012-07-14T00:00:00.0000Z"
   "abc 2012-07-14T00:00:00.1234Z"

Summary

What have we learnt?

  • A string field containing a date-time in round-trip format will be truncated when updated via the WebApi as long as it will not cause a loss in precision.
  • The truncation is implemented by the WebApi on the server (not in any client or library).
  • The IOrganisationService does not truncate string fields.
  • The truncation occurs on all types of string column, i.e. “Text”, “Text Area” and “Multi-line Text”.