1

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.

Frontmaniaac
  • 221
  • 3
  • 14
  • The selection screen can be changed under the AT SELECTION-SCREEN OUTPUT event, but to be able to add a precise answer (with working code), some code from you would be useful (what have you tried?). – József Szikszai Sep 22 '22 at 11:44
  • i added a simple example, and i meant obligatory not obsolete sorry – Frontmaniaac Sep 22 '22 at 12:09
  • I have tried this: INITIALIZATION. LOOP AT SCREEN. IF SCREEN-name = p_del. screen-required = 2. ENDIF. MODIFY SCREEN. ENDLOOP. – Frontmaniaac Sep 22 '22 at 12:28
  • Does this answer your question? [How to skip mandatory fields on selection screen?](https://stackoverflow.com/questions/15084916/how-to-skip-mandatory-fields-on-selection-screen) – Suncatcher Oct 07 '22 at 01:28

1 Answers1

5

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.
József Szikszai
  • 4,791
  • 3
  • 14
  • 24
  • I would suggest to change it to this. Because earlier it would not turn back to without obligatory. AT SELECTION-SCREEN ON p_del. LOOP AT SCREEN. IF screen-group1 = 'NUM'. IF p_del = abap_true. screen-required = 1. ELSE. screen-required = 0. ENDIF. MODIFY SCREEN. ENDIF. ENDLOOP. – Frontmaniaac Oct 13 '22 at 12:07