ABAP – Load user-specific selection screen variant during program initialization

When user starts a program/report in SAPGUI, he want’s to have some speicific fields to be pre-filled with fixed values. This is true especially on more complex selection screens. So he must either create a transaction which executes the report with selected variant, or he can start the program directly and choose the variant on the selection screen manually.

But there’s also another option – load the variant dynamically by ABAP coding, during the INITIALIZATION event…The code initially tries to find user-specific variant.

If it’s not successful, it tries to load STANDARD variant with default values preset for all users.

FORM load_selection_screen_variant.

  DATA:
    lv_subrc   TYPE sy-subrc,
    lv_variant TYPE rsvar-variant.

  lv_variant = |U_{ sy-uname }|.

* Try to read user-specific variant
  CALL FUNCTION 'RS_VARIANT_EXISTS'
    EXPORTING
      report  = sy-repid
      variant = lv_variant
    IMPORTING
      r_c     = lv_subrc.

* User specific variant does not exist - try to read default variant instead
  IF lv_subrc IS NOT INITIAL.
*          *****        System-Variante
    lv_variant = 'STANDARD'.

    CALL FUNCTION 'RS_VARIANT_EXISTS'
      EXPORTING
        report  = sy-repid
        variant = lv_variant
      IMPORTING
        r_c     = lv_subrc.
  ENDIF.

* We have eiterh user-specific or default variant -> load selection screen
* with data from the selected variant
  IF lv_subrc = 0.
    CALL FUNCTION 'RS_SUPPORT_SELECTIONS'
      EXPORTING
        report               = sy-repid
        variant              = lv_variant
      EXCEPTIONS
        variant_not_existent = 01
        variant_obsolete     = 02.
  ENDIF.
ENDFORM.

In the INITIALIZATION event, just call the above form to load the variant

INITIALIZATION.
  IF sy-slset IS INITIAL.  "Selektionsbilder-> Variant name    
    PERFORM selection_var_find.                               
  ENDIF.

Leave a Reply