ABAP – Select ALV layout on the selection screen

If you want to offer your users to have a parameter on the selection screen where they can select layout of the ALV to be displayed, you can use the following code snippet

The parameter on the selection screen:

SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-b01.
  PARAMETERS p_layo LIKE disvariant-variant.
SELECTION-SCREEN END OF BLOCK b01.

When your report is started, you can pre-fill the p_layo parameter with value of the DEFAULT layout in the INITIALIZATION event:

INITIALIZATION.
PERFORM get_default_layout CHANGING p_layo.

FORM get_default_layout CHANGING cv_layout TYPE disvariant-variant.
  DATA:
    ls_variant TYPE disvariant.

  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
    EXPORTING
      i_save        = 'A'
    CHANGING
      cs_variant    = ls_variant
    EXCEPTIONS
      wrong_input   = 1
      not_found     = 2
      program_error = 3
      OTHERS        = 4.
  IF sy-subrc = 0.
    cv_layout = ls_variant-variant.
  ENDIF.
ENDFORM.

To allow user to select from all saved variants, just hook the ON VALUE-REQUEST to the selection screen parameter:

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_layo.
  perform select_alv_variant CHANGING p_layo.

FORM select_alv_variant CHANGING cv_layout TYPE disvariant-variant.
  DATA:
    ls_variant TYPE disvariant.

  ls_variant-report = sy-repid.

  CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
    EXPORTING
      is_variant    = ls_variant
      i_save        = 'A'
    IMPORTING
      es_variant    = ls_variant
    EXCEPTIONS
      not_found     = 1
      program_error = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    cv_layout = ls_variant-variant.
  ENDIF.
ENDFORM.

Leave a Reply