cancel
Showing results for 
Search instead for 
Did you mean: 

Mark Date field as Nullable in .cds file for CAPM project

raavi
Explorer
0 Kudos

Dear Colleagues,
I am building a CAPM project where I have Date type(type: Date or Timestamp) field in my cds. I want this field to be Nullable because whenever I try to save the UI with null values it throws error as below

Deserialization Error: Invalid Value Z(String) for property..... 

raavi_0-1712246591817.png

Since, Date field is optional I would like to mark it as Nullable so that error is no shown while creating the records.

 

Kindly Help.

 

#capm #cds

 

View Entire Topic
RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Kudos

Apparently your UI sends "OnStockDate":"Z" to the backend if the input field is empty instead of sending "OnStockDate":null.

 

raavi
Explorer
0 Kudos
Dear @RalfHandl,
raavi
Explorer
0 Kudos
Dear @RalfHandl, I checked the UI, and it is not sending 'Z' as value. In network I can see payload as "OnStockDate": ""
RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Kudos

Well, "OnStockDate":"" is as wrong as "OnStockDate":"Z" would be. The UI must send "OnStockDate":null if the date should be null.

raavi
Explorer
0 Kudos
Dear @RalfHandl, Since on UI since I am not entering any value in the date input field, so it is expected that it will take the blank value, and in the payload I can see the blank value. However framework throws error with blank value. How and Why will the UI pass null ?
RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Kudos
How to make the UI pass null depends on the UI techonology you are using. Please add this information to your question.
raavi
Explorer
0 Kudos
No, what I mean is, why is it expecting me pass a null value? Can't I simply keep the Date field as blank ?
raavi
Explorer
0 Kudos
What I believe is in Odata we have an option to add a nullable property to our Date fields, if we want to keep it blank, however I see that option is not available here. Is it possible to mark Date field as Nullable so that it accepts blank values as well ?
raavi
Explorer
0 Kudos
Dear @RalfHandl, Is it necessary to pass null for Date field, is it not possible to add annotation in CDS so that it will accept blank/empty values?
RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Kudos
If you define an element in CAP CDS without the "not null" phrase, it is nullable. Which means it will accept JSON null as a value, which is different from an empty JSON string. There is no magic switch to treat empty strings identical to null.
raavi
Explorer
0 Kudos
@RalfHandl, but I also have fields of type String and Integer which are defined with Not Null Phrase, but when empty Json string is accepted here, but same is not true Date field. I am using SAPUI5 as frontend, and I think it is giving empty strings as default for input fields
RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Kudos

An empty string is a string and it is not null.

An "empty" integer typically is zero which again is different from null.

Unfortunately I don't know how to tell SAPUI5 to treat an empty date field as null.