loopsabapsap-erpdynprosap-selection-screens

Make 0 recognized as 'not initial' by ABAP?


I try to learn ABAP and code my first simple calculator using parameters.

However, I have stumbled upon a problem. I want an error to be shown when some parameters are empty, i.e. when nothing was entered by a user, so it should not be possible to pass without a number(s).

But the number 0 is also being picked up as no number, because seeing it from a math perspective, 0 is nothing, however, in my case I want 0 to be picked up as "something".

PARAMETERS: inp_z1   TYPE p DECIMALS 2 DEFAULT 1,
            operator,
            inp_z2   TYPE p DECIMALS 2 DEFAULT 1.

DATA: inp_erg       TYPE p DECIMALS 10,
      inp_ergstr    TYPE string,
      error_boolean.

IF inp_z1 IS INITIAL OR inp_z2 IS INITIAL.
  WRITE: / TEXT-r01.
  error_boolean = 'X'.
ELSE.
 ...

I tried to use IF inp_z1 < 0 or inp_z2 < 0, but it does not allow me to use negative numbers.


Solution

  • You cannot use some numeric value to connotate an additional special meaning other that the number this value represents, because there is no such value exists.

    The predicate expression IS INITIAL checks whether the operand is initial, and it is true, if the operand contains its type-friendly initial value. For the p type initial value is 0, so this check is equivalent to check if the value of operand is 0.

    If you need to make your field required to enter some value, you can do it in several ways, depending on your requirements. One way is to add to each of your mandatory parameters keyword OBLIGATORY, to statically set the attribute to indicate a field as a required one:

    PARAMETERS: inp_z1   TYPE p DECIMALS 2 DEFAULT 1 OBLIGATORY,
                operator OBLIGATORY,
                inp_z2   TYPE p DECIMALS 2 DEFAULT 1 OBLIGATORY.
    

    Another way is to set this attribute dynamically. In your case, as there are several fields which must be entered by user, it is better to assign all of them to one group with a keyword MODIF ID ... and change the property for the group.

    Assign a group i01 to all mandatory fields:

    PARAMETERS: inp_z1   TYPE p DECIMALS 2 DEFAULT 1 MODIF ID I01,
                operator MODIF ID i01,
                inp_z2   TYPE p DECIMALS 2 DEFAULT 1 MODIF ID i01.
    

    Afterwards in the event AT SELECTION-SCREEN OUTPUT it is necessary to set the required property screen-required dynamically (you can just place the following code below parameters block). Note that the group name in the condition should be always uppercase, i.e. I01:

    AT SELECTION-SCREEN OUTPUT.
      LOOP AT SCREEN.
        IF screen-group1 = 'I01'.
          screen-required = '1'.
          MODIFY SCREEN.
        ENDIF.
      ENDLOOP.
    

    P.S. you can also use the same construction to change the properties of individual fields, i.e.:

     ...
        IF screen-name = 'INP_Z1'.
          screen-required = '1'.
          MODIFY SCREEN.
        ENDIF.
     ...
    

    And afterwards in the standard-event START-OF-SELECTION you can program your logic. Just place

    START-OF-SELECTION.
    

    after the previous block and write your code afterwards.