# ABAP Selection Screen and SELECT-OPTIONS Generator

> Source: https://netray.co/tools/abap-selection-screen-select-options-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Builds a complete ABAP report with a framed selection screen, two SELECT-OPTIONS, a parameter, radio buttons that hide a field, event blocks, validation, an optional option restriction and an ALV output.

## Category

developer

## Erp

SAP NetWeaver / Basis

## Audience

- ABAP developer
- SAP technical consultant

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Report name

**Key:** report

**Type:** text

**Default:** ZMM_STOCK_REPORT

#### Table for field types (the SELECT reads MARD)

**Key:** tab

**Type:** text

**Default:** mard

#### Select-option 1 field

**Key:** so1

**Type:** text

**Default:** matnr

**Help:** Name s_<field> must stay within 8 characters.

#### Select-option 2 field (plant)

**Key:** so2

**Type:** text

**Default:** werks

#### Parameter declaration

**Key:** param

**Type:** text

**Default:** p_lgort TYPE mard-lgort

#### Addition for select-option 2

**Key:** oblig

**Type:** text

**Default:** OBLIGATORY

**Help:** Clear to make it optional.

#### Display option for select-option 2

**Key:** intv

**Type:** select

**Options:**

- NO INTERVALS
- NO-EXTENSION
- NO INTERVALS NO-EXTENSION

**Default:** NO INTERVALS

#### Default plant

**Key:** plant

**Type:** text

**Default:** 1000

**Blocks:**

#### Report with selection screen

**Body:** REPORT {{report}}.

DATA gv_{{so1}} TYPE {{tab}}-{{so1}}.
DATA gv_{{so2}} TYPE {{tab}}-{{so2}}.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
  SELECT-OPTIONS: s_{{so1}} FOR gv_{{so1}},
                  s_{{so2}} FOR gv_{{so2}} {{oblig}} {{intv}}.
  PARAMETERS {{param}}.
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_{{so2}}[] = VALUE #( ( sign = 'I' option = 'EQ' low = '{{plant}}' ) ).

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_{{so2}}.
  " plant check: adjust if select-option 2 is not WERKS
  SELECT SINGLE werks FROM t001w
    WHERE werks IN @s_{{so2}}
    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 {{so1}} IN @s_{{so1}}
      AND {{so2}} IN @s_{{so2}}
    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_{{so1}} to EQ and BT, include only (add at the end of INITIALIZATION)

**Body:**   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_{{so1}}' ).
  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)

**Table:**

**Columns:**

- Element
- Text

**Rows:**

**0:** Text symbol 001

**1:** Selection criteria

**0:** Text symbol 002

**1:** Output options

**0:** Selection text S_{{so1}}

**1:** Material (or tick Dictionary Ref.)

**0:** Selection text S_{{so2}}

**1:** Plant (or tick Dictionary Ref.)

**0:** Selection text P_ALL / P_MIN / P_MINQ

**1:** All stock / Minimum stock only / Minimum unrestricted qty

**Notes:** 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.

## Content

**Intro:** 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.

**Sections:**

#### How to use it

**Body:** 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.

**Bullets:**

- 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

**Body:** 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

**Body:** 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

**Body:** 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.

**Cta:** 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.

**Faq:**

#### How do I declare SELECT-OPTIONS in SAP ABAP?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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.

## Search terms

abap select options, abap report selection screen, how to declare select options in sap abap, at selection screen event example in sap abap, abap select_options_restrict, dynamic selection screen in sap abap example, abap selection screen block with frame

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
