ABAP Selection Screen and SELECT-OPTIONS Generator
This generator writes a runnable ABAP report with a framed selection screen, two SELECT-OPTIONS, a parameter, radio buttons that hide a field, validation and an ALV list. It targets SAP NetWeaver 7.40 SP08 and later, including S/4HANA.
Fill in
Name s_<field> must stay within 8 characters.
Clear to make it optional.
Report with selection screen
REPORT ZMM_STOCK_REPORT.
DATA gv_matnr TYPE mard-matnr.
DATA gv_werks TYPE mard-werks.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: s_matnr FOR gv_matnr,
s_werks FOR gv_werks OBLIGATORY NO INTERVALS.
PARAMETERS p_lgort TYPE mard-lgort.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002.
PARAMETERS: p_all RADIOBUTTON GROUP g1 DEFAULT 'X' USER-COMMAND uc,
p_min RADIOBUTTON GROUP g1.
PARAMETERS p_minq TYPE mard-labst MODIF ID m1.
SELECTION-SCREEN END OF BLOCK b2.
INITIALIZATION.
s_werks[] = VALUE #( ( sign = 'I' option = 'EQ' low = '1000' ) ).
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN INTO DATA(ls_screen).
IF ls_screen-group1 = 'M1'.
ls_screen-active = COND #( WHEN p_min = 'X' THEN 1 ELSE 0 ).
MODIFY SCREEN FROM ls_screen.
ENDIF.
ENDLOOP.
AT SELECTION-SCREEN ON s_werks.
" plant check: adjust if select-option 2 is not WERKS
SELECT SINGLE werks FROM t001w
WHERE werks IN @s_werks
INTO @DATA(lv_werks).
IF sy-subrc <> 0.
MESSAGE 'No valid plant in the selection' TYPE 'E'.
ENDIF.
START-OF-SELECTION.
SELECT matnr, werks, lgort, labst
FROM mard
WHERE matnr IN @s_matnr
AND werks IN @s_werks
INTO TABLE @DATA(lt_mard).
IF p_lgort IS NOT INITIAL.
DELETE lt_mard WHERE lgort <> p_lgort.
ENDIF.
IF p_min = 'X'.
DELETE lt_mard WHERE labst < p_minq.
ENDIF.
TRY.
cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lo_alv)
CHANGING t_table = lt_mard ).
lo_alv->get_functions( )->set_all( abap_true ).
lo_alv->display( ).
CATCH cx_salv_msg INTO DATA(lx_salv).
MESSAGE lx_salv TYPE 'I' DISPLAY LIKE 'E'.
ENDTRY.Optional: restrict s_matnr to EQ and BT, include only (add at the end of INITIALIZATION)
DATA: ls_restrict TYPE sscr_restrict,
ls_opt_list TYPE sscr_opt_list,
ls_ass TYPE sscr_ass.
ls_opt_list-name = 'EQ_BT'.
ls_opt_list-options-eq = abap_true.
ls_opt_list-options-bt = abap_true.
APPEND ls_opt_list TO ls_restrict-opt_list_tab.
ls_ass-kind = 'S'.
ls_ass-name = to_upper( 's_matnr' ).
ls_ass-sg_main = 'I'.
ls_ass-sg_addy = space.
ls_ass-op_main = 'EQ_BT'.
APPEND ls_ass TO ls_restrict-ass_tab.
CALL FUNCTION 'SELECT_OPTIONS_RESTRICT'
EXPORTING
restriction = ls_restrict
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE 'Selection restriction failed' TYPE 'I'.
ENDIF.Text elements to create (SE38 > Goto > Text Elements)
| Element | Text |
|---|---|
| Text symbol 001 | Selection criteria |
| Text symbol 002 | Output options |
| Selection text S_matnr | Material (or tick Dictionary Ref.) |
| Selection text S_werks | Plant (or tick Dictionary Ref.) |
| Selection text P_ALL / P_MIN / P_MINQ | All stock / Minimum stock only / Minimum unrestricted qty |
Requires ABAP 7.40 SP08 or later for inline declarations, VALUE and the new Open SQL syntax with @ host variables. Verify in SE37 that SELECT_OPTIONS_RESTRICT and types SSCR_RESTRICT, SSCR_OPT_LIST and SSCR_ASS exist with these components on your release; the function must be called in INITIALIZATION. Field names are case-insensitive in ABAP, but the restriction NAME must be the upper-case select-option name, so the code converts it with to_upper.
Get your Z report reviewed
Send us the selection screen and SELECT and we will check performance and authorization handling before it goes to production.
Optional. The tool and every export work without this. No spam.
How to use it
Enter the report name, the table and the two fields to select on, the parameter declaration and whether the second select-option is obligatory or shown without intervals. Paste the code into SE38 or ADT, create the text symbols and selection texts listed in the third block, activate and run. The default build is a stock report on MARD by material and plant with an optional storage location and a minimum-quantity switch. Add the optional block inside INITIALIZATION when users should only be allowed single values and ranges for the first select-option.
- SELECT-OPTIONS names are limited to 8 characters.
- Declare the FOR reference as a data object; TABLES is not needed.
- Use selection texts with Dictionary Ref. for translated labels.
Selection screen events in order
INITIALIZATION runs once before the screen is first shown: set defaults such as the plant range here, and call SELECT_OPTIONS_RESTRICT here, because later it raises TOO_LATE. AT SELECTION-SCREEN OUTPUT is the screen PBO and runs before every display; this is where LOOP AT SCREEN and MODIFY SCREEN hide or show fields, which is what most people mean by a dynamic selection screen. After the user presses Enter or F8, AT SELECTION-SCREEN ON <field> runs for each field, then AT SELECTION-SCREEN for the whole screen; an error message there sends the user back with the field open for input. START-OF-SELECTION runs only when all checks pass.
Worked example
With defaults, the screen shows Material as a range, Plant as a single-value list prefilled with 1000 and marked obligatory, and Storage location. Choosing Minimum stock only triggers the USER-COMMAND on the radio group, the PBO runs again and the quantity field with MODIF ID M1 appears. If a user enters plant 9999, the ON s_werks check finds no T001W row and raises an error on that field only. The SELECT uses IN @s_matnr and IN @s_werks, so an empty material range selects all materials, which is the standard behaviour of SELECT-OPTIONS and a common surprise.
Gotchas
An empty select-option means no restriction, so an unrestricted report can read the whole table; make the key field obligatory or check it in AT SELECTION-SCREEN. A select-option is an internal table with header line: use s_werks[] to address the table. Large IN ranges with many single values can exceed database statement limits; push big lists through a FOR ALL ENTRIES or a join instead. Messages of type E in START-OF-SELECTION end the program instead of returning to the screen, so validate earlier. Missing text elements show the technical names on screen.
Frequently Asked Questions
How do I declare SELECT-OPTIONS in SAP ABAP?
Declare a data object of the field type, for example DATA gv_matnr TYPE mard-matnr, then SELECT-OPTIONS s_matnr FOR gv_matnr. Additions such as OBLIGATORY, NO INTERVALS, NO-EXTENSION, DEFAULT and MODIF ID control the screen. Use the select-option in SQL with WHERE matnr IN @s_matnr.
What is an example of AT SELECTION-SCREEN in ABAP?
AT SELECTION-SCREEN ON s_werks with a SELECT SINGLE against T001W and MESSAGE TYPE 'E' when nothing is found validates the plant field and returns the user to it. AT SELECTION-SCREEN OUTPUT with LOOP AT SCREEN changes field attributes before the screen is displayed.
How do I use SELECT_OPTIONS_RESTRICT?
Call it in INITIALIZATION with a RESTRICTION structure of type SSCR_RESTRICT. Fill OPT_LIST_TAB with a named list of allowed options (EQ, BT and so on) and ASS_TAB with the select-option name, kind S, the allowed signs and the option list name. Called later it raises TOO_LATE.
How do I make a dynamic selection screen in ABAP?
Give fields a MODIF ID, attach USER-COMMAND to a radio button group or checkbox, and in AT SELECTION-SCREEN OUTPUT loop over SCREEN and set ACTIVE, INPUT or INVISIBLE for that group, then MODIFY SCREEN. For fully dynamic field lists, logical database free selections or FREE_SELECTIONS function modules are the heavier option.
If you want your users to ask SAP stock and order questions in plain English instead of waiting for a new Z report, we can run that on your own servers, read-only.
Related Tools
ABAP CDS Table Function with AMDP Generator
Builds the three parts of a CDS table function: the DDL definition with client handling and parameters, the AMDP class with SQLScript, and the ABAP SQL call that consumes it.
SAPABAP ALV Report Skeleton Generator
Enter a program name, table and selection field and get a working ABAP report that shows an ALV grid using CL_SALV_TABLE.
SAPABAP SELECT Statement Generator
Fill in table, fields and a WHERE condition and get modern ABAP Open SQL: SELECT INTO TABLE, SELECT SINGLE and a safe FOR ALL ENTRIES pattern.
Developer ToolsSAP GUI Scripting Script Generator (Python win32com)
Builds a runnable Python script that drives SAP GUI for Windows through the SAP GUI Scripting API: connect, log on to a client, call a transaction, set a field and capture the result.
More Developer Tools tools
See all- ABAP New Open SQL Syntax Reference by Release (7.40 to 7.58)
- ABAP ALV Layout, Field Catalog and Color Codes Reference
- SAP Gateway SEGW DPC_EXT CRUD Methods Generator (ABAP)
- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
Keep exploring
Free tools
- ABAP ALV Report Skeleton GeneratorEnter a program name, table and selection field and get a working ABAP report that shows an ALV grid using CL_SALV_TABLE.
- ABAP ALV Layout, Field Catalog and Color Codes ReferenceSide by side: how to do checkbox, hotspot, colors, subtotals, toolbar buttons, top of page and refresh in CL_GUI_ALV_GRID and in CL_SALV_TABLE, plus the C1 to C7 color codes.
- SAP Configurable Material Selection ABAP GeneratorFill in plant and material type to get an ABAP report that lists configurable materials and counts them by plant.