abapsap-selection-screens

How can I change a field to be obligatory, after the value of one field has changed?


I am struggling to find out how to change a field to be obligatory on a selection screen, but I want it to change dynamically based on a checkbox that is marked.

So for context I have a program with two options in the selection screen. So when I select the first checkbox, I want one of the fields to become obligatory, and when I select the other checkbox for the other option of the program I don't want the field to be obsolete, because the program won't use the value anyway so it does not matter.

Example code:

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. 
PARAMETERS: p_sel AS CHECKBOX,
            p_num TYPE i. 
SELECTION-SCREEN END OF BLOCK b1.    
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME. 
PARAMETERS: p_del AS CHECKBOX,
            p_num2 TYPE i. 
SELECTION-SCREEN END OF BLOCK b2.
"I want to do something like
INITIALIZATION.
LOOP AT SCREEN.
 IF SCREEN-name = p_del AND p_del = abap_true.
  screen-required = 2.
 ENDIF.
MODIFY SCREEN.
ENDLOOP.

But this does not seem to work

So when I select p_del I want p_num2 to become OBLIGATORY.

Thanks ahead.


Solution

  • I added comments to the lines I changed.

    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
    PARAMETERS: p_sel AS CHECKBOX USER-COMMAND uc01, " USER-COMMAND technicall necessary that SAP recognises the checkbox click
                p_num TYPE i MODIF ID num. " MODIF ID 'NUM' will be used later
    SELECTION-SCREEN END OF BLOCK b1.
    SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
    PARAMETERS: p_del  AS CHECKBOX,
                p_num2 TYPE i.
    SELECTION-SCREEN END OF BLOCK b2.
    
    
    AT SELECTION-SCREEN OUTPUT. " Use this event (not INITIALIZATION)
    
      LOOP AT SCREEN.
        IF screen-group1 EQ 'NUM'. "MODIF ID used here to turn required on/off 
          IF p_sel EQ abap_true. " If checkbox is checked
            screen-required = '1'.
          ELSE.
            screen-required = '0'.
          ENDIF.
        ENDIF.
        MODIFY SCREEN.
      ENDLOOP.