Search code examples
odataabapsap-gateway

Pass an empty value to an OData V2 Edm.Time property


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?


Solution

  • 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.