Skip to Content
Technical Articles

DateTime field in OData entity – Points to take care when the UI is throwing run-time error.

Introduction:

Normally when there is a field (for e.g. DateChanged) of type DateTime in the OData entity, we might face few issue in the front-end

  1. if this field has a null value entry, at this point of time the frontend UI application will throw a run-time error. Here in this blog post we are going to see how to handle this issue of DateTime field in the back-end, so that we don’t face any issue while loading the UI application.
  2. Also there there might be a situation when we tend to show only the Date value in the UI for a field removing the Time from it.

 

  • Steps to take care when the DateTime field value can be null/empty

Step 1: If the properties of an entity in the OData is editable, then make sure to check the checkbox for the option “Nullable”

Step 2:  If the properties of an entity are non-editable, for e.g., when the OData is created through the CDS view). Here the property can be handled by redefining the DEFINE method of MPC_EXT  class of the OData

 

  • How to display a DateTime field in Date format in the UI application.

Simple step here is to redefine the DEFINE method of MPC_EXT class of the OData and add the below code, so that the display format in the UI is ‘Date’

 

Conclusion:

Now you will be able to display the date field of an entity in OData in Date format instead of DateTime format in the UI. And also you will be able to overcome the run-time error which was caused because of DateTime field

1 Comment
You must be Logged on to comment or reply to a post.
  • Hi,

    Thanks for your blog, but you can add another problem we have with date in the UI. When using datetime type you add hours in it and in the browser it display the date according to the UTC timestamp, so in some case you have one day of difference.

    Regards,

    Joseph