2

I have a variable type time but sometimes this variable doesn't have anything.

When it is initial, it shouldn't be "000000", I want an empty value without anything (no zeros). Let me explain my problem with the code:

IF lwa_hora IS INITIAL.
   CLEAR lwa_hora.
   ls_entity-hora = lwa_hora. " Result: 000000 but I don't want any zero
ELSE.
   ls_entity-hora = lwa_hora. " Result: 000000
ENDIF.

I tried with CLEAR but nothing happens.

I need this is because in the JavaScript frontend client logic, I need the OData property to contain a falsy value (E.g. null or an empty string "").

But it always has the value "000000" which is not an empty string. Is it possible to make something in the backend to "clear" the variable?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
kyara
  • 133
  • 6

3 Answers3

6

The time data-type in abap (t) is a value-type. It's internally implemented as an integer counting the seconds since midnight. 0 seconds since midnight is a valid value, so it can't have a null-value.

However, ABAP allows you to create a reference to any value-type:

hora TYPE REF TO t.

That means hora will be a reference to a variable of TYPE t. Initially, this reference will be unbound, which is conceptually very similar to a null-reference in other programming languages. You can check that with:

IF ls_entity-hora IS BOUND.
...
IF ls_entity-hora IS NOT BOUND.

You can assign a time value with GET REFERENCE OF lwa_hora INTO ls_entity-hora. But now you have a reference to an existing variable. Change the value of lwa_hora, and the value of ls_entity-hora also changes. That might not always be what you want. So it might be better to create a new piece of data in memory for our reference to point to:

CREATE DATA ls_entity-hora.

Now ls_entity-hora is no longer unbound (or "null" if you want to call it that), it points to a new time-value of 000000. If you want to read or change the value of this nameless piece of data this reference points to, then you can do this with the dereferencing-operator ->*:

ls_entity-hora->* = lwa_hora.

If you intentionally want to set a reference to unbound (or "set the reference to null"), you can do that by clearing the reference:

CLEAR ls_entity-hora.

By the way: Representing a point in time by two separate variables of the types d and t fell out of fashion in the past decade. The current best practice for this situation is to use a single variable of type TIMESTAMP (if you need second precision) or TIMESTAMPL (if you need microsecond precision). A timestamp of 00000000000000 is obviously an illegal value, so it can be used to represent the absence of a point in time. This type also usually makes it much easier to communicate with a SAPUI5 frontend (like in your case), because many of the technologies for making oData services offer automatic conversion between Javascript Date and ABAP TIMESTAMP.

Philipp
  • 67,764
  • 9
  • 118
  • 153
1

An alternative to heap allocating the time would be to store a boolean next to it, indicating whether it is set or not:

TYPES:
     BEGIN OF optional_time,
        time    TYPE t,
        is_null TYPE abap_bool,
     END OF optional_time.

DATA(no_time) = VALUE optional_time( is_null = abap_true ).

" Setting to null:
DATA(some_time) = no_time.
" Setting to some time:
some_time = VALUE #( time = '12:30' ).

IF some_time = no_time.
   " ...
ENDIF.
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

This sort of things is probably better to handle on front-end than on back-end.

SAP Gateway serializes ABAP Date/time initial values to NULL in the OData response if the corresponding property is nullable. You need to make sure this property is set to TRUE like in this sample

enter image description here

you can also set this property in runtime

TRY .
 lo_action = model->get_entity_type( iv_entity_name = 'Z_REPORTType').                                                             
 lo_property = lo_action->get_property( iv_property_name = 'Requested_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.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90