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
- 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.
- 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
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
Hello Experts,
I have a similar requirement and despite trying to force feed the value in MPC_EXT still I am unable to override the nullable property.
I keep on getting the null date error still.
Any leads will be highly appreciated.
Code sample attached for reference.
super->define( ).
TRY .
lo_action = model->get_entity_type( iv_entity_name = 'ZCMM_LSP_REPORT_PARENTType').
* CATCH /iwbep/cx_mgw_med_exception. " Meta data exception.
lo_property = lo_action->get_property( iv_property_name = 'Requested_Date').
lo_property->set_nullable(
iv_nullable = abap_true
).
lo_property = lo_action->get_property( iv_property_name = 'GAC_Date').
lo_property->set_nullable(
iv_nullable = abap_true
).
CATCH /iwbep/cx_mgw_busi_exception /iwbep/cx_mgw_med_exception /iwbep/cx_mgw_tech_exception INTO DATA(lo_exception).
ENDTRY.
You need to clear the model cache for the used model using /iwfnd/cache_cleanup then it should work, this did for me the trick.
Yes, setting the date field to Nullable does not fix the runtime error with empty dates.
I'm not sure why this blog is published with wrongful solution.