Selection Screen Events in SAP ABAP
Selection screen events in SAP ABAP handle user input validation, dynamic screen changes, and program logic execution based on user interaction.
The Events are :
LOAD-OF-PROGRAM
INITIALIZATION
AT SELECTION-SCREEN OUTPUT
AT SELECTION SCREEN
AT SELECTION-SCREEN ON VALUE REQUEST
AT SELECTION-SCREEN ON HELP REQUEST
AT SELECTION-SCREEN ON FIELD
START-OF-SELECTION
END-OF-SELECTION
TOP-OF-PAGE
END-OF-PAGE
In this Blog, we understand Initialization, At Selection-screen output, and At Selection-screen events.
Initialization
This event is triggered before the screen is displayed to the user. The event is used to set the default values for fields.When the user sees the selection screen, these values will be filled in by default.
REPORT zexample.
PARAMETERS: p_date TYPE sy-datum, " A date field on the selection screen
p_name TYPE char20. " A name field on the selection screen
INITIALIZATION.
p_date = sy-datum. " Set the default value of the date to the current system date
p_name = 'ABC'. " Set the default name to 'ABC'
At Selection Screen Output
At selection screen output event is triggered before the screen displays to the user and also when the screen refreshes.Every time the user interacts with the screen, refreshment will happen.
So this event is used to modify the selection screen dynamically before it is displayed and also when the user interacts with the screen because it refreshes the screen, Hence it allows you to control the appearance and behavior of the selection screen dynamically. You can hide or display fields, change field labels, or make fields mandatory or read-only based on certain conditions.
let’s understand with an example,
I have declared two radio buttons and three checkboxes, by default radio button P_radio1 is clicked. Test Case is if I clicked p_radio1 my first checkbox should be visible , if clicked on the second p_radio2 then the second and third checkboxes should be visible.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_radio1 RADIOBUTTON GROUP rad1 DEFAULT 'X' USER-COMMAND UC, " First radio button
p_radio2 RADIOBUTTON GROUP rad1. " Second radio button
PARAMETERS: p_chk1 AS CHECKBOX MODIF ID AA, " First checkbox
p_chk2 AS CHECKBOX MODIF ID BB, " Second checkbox
p_chk3 AS CHECKBOX MODIF ID BB. " Third checkbox
SELECTION-SCREEN END OF BLOCK b1.
MODIF ID is used to assign the group to selection screen parameters. By MODIF ID you can include more than one field in the group.
Whenever the radio button and checkbox are ticked it will have the value ‘X‘.
AT SELECTION-SCREEN OUTPUT.
IF p_radio1 = 'X'.
LOOP at screen.
IF screen-group1 = 'BB'.
screen-active = 0.
Modify screen.
ENDIF.
ENDLOOP.
elseif p_radio2 = 'X'.
LOOP at screen.
IF screen-group1 = 'AA'.
screen-active = 0.
Modify screen.
ENDIF.
ENDLOOP.
endif.
SCREEN is a SAP standard structure that will hold data related to the selection screen or the screen elements.
Screen-group1 is an element used to group multiple fields together so that actions can be applied to the entire group at once. Here, p_chk1 is under one group which is assigned using modif id ‘AA‘, and p_chk2 and p_chk3 are under the group which is assigned using modif id ‘BB‘.
Screen-active controls whether a field is active (visible and editable) on the screen.
If Screen-active is equal to zero that field will be hidden and vice-versa.
Instead of using screen-active, we can use elements screen-input and screen-invisible together
IF p_radio1 = 'X'.
LOOP at screen.
IF screen-group1 = 'BB'.
screen-input = 0.
screen-invisible = 1.
Modify screen.
ENDIF.
ENDLOOP.
if screen-input is zero then that field is uneditable and vice-versa.if screen-invisible is one then that field is hidden and vice-versa.
So in this way, you can control the visibility of screen elements based on user interaction in the selection-screen.
At Selection-Screen.
At selection-screen event is triggered when the user gives input and presses enter then this event triggers.
Basically, This event triggers after user input.
This event is used for data validations, handling errors, and implementing logic right after the user interacts with the selection screen. It ensures correct data input and manages conditional behavior based on the user’s selections.
PARAMETERS: p_name TYPE char20,
p_age TYPE i.
AT SELECTION-SCREEN.
" Validate that age is a positive number
IF p_age < 0.
MESSAGE 'Age must be a positive number' TYPE 'E'.
ENDIF.
" Validate that name is not empty
IF p_name IS INITIAL.
MESSAGE 'Name cannot be empty' TYPE 'E'.
ENDIF.
NOTE:
Both events are used for the selection screen. But the Main difference is
Parameters : p_mtart type mara-mtart.
at selection-screen output.
if p_mtart = 'FERT'.
MESSAGE: 'INVALID DATA' TYPE 'E'.
ENDIF.
If we write the validation logic in at selection-screen output event, the Message type ‘E’ will not allow the user to proceed with the program flow. It will come out of the transaction.
Parameters : p_mtart type mara-mtart.
at selection-screen.
if p_mtart = 'FERT'.
MESSAGE: 'INVALID DATA' TYPE 'E'.
ENDIF.
If we write the validation logic in at selection-screen event, it will validate the fields and if any error occurs, the message will be displayed. The program flow will not be disturbed.
That's all about EVENTS and thanks for reading hope you have enjoyed it.
Subscribe to my newsletter
Read articles from Vidhi Chijwani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by