ABAP – Using dynamic variables in program selection screen variants

SAPI believe you once had to solve a requirement (if not, you’ll definitely receive one in future) where you had to design a SELECTION-SCREEN parameters which value has to be dynamically updated according to results of the program – eg. last processed IDOC number, Purchase/Sales Order number or just the last date when the report was executed.

One solution is to create a Z-table which will contain just one entry with value you need.

Another, more clean, clear and easy to maintain solution is to use dynamic variables in your variant SELECTION-SCREEN PARAMETERS or SELECT-OPTIONS.

All existing program variants are all stored in table VARI.
Variant texts (descriptions) are stored in table VARIT.
Variant directory structure info is stored in table VARID.
Assignments of variants to selection screens are stored in table VARIS.

The problem is that the select-options and parameters values are stored in a cluster thus they are not easilly accessible – you can’t write/read them easily. It is of course possible using function modules starting on RS_VARIANT*, eg.

  • RS_VARIANT_CONTENTS – returns all parameters and select-options with their assigned values for given report and its variant
  • RS_VARIANT_VARIABLES – returns all variable values used as a value for a parameter or a select-option for given report and its variant
  • …and many others, which allow you to get data about variants and manipulate them.

But back to our problem. Let’s assume you would like to have a selection screen with three fields:

  • IDOC number with value of last IDOC number processed
  • Date with value of last report run’s date
  • Time with value of last report start’s time

You can achieve this using the RS_VARIANT* function modules: you read all variant parameters and select-options values, modify them in program and update the variant with new values back to cluster using another RS_VARIANT* function module.

More elegant solution would be a variable stored in table TVARVC, which serves as a “repository” for all dynamic variables that you can use in your variants:

  1. Create new entries using TCode SM30 in TVARV view (maintenance of TVARV is rediredcted to TVARVC – this is because TVARV become obsolete due to lack of client dependency field).
    Creation of an entry in TVARVC can also be done directly by the program, but note you can assign only the existing entries in TVARVC to program variants
    New variables in TVARVC
  2. You can include the changed entries in a transport request so you can easily transport the variant variables
  3. In SE38 prepare selection screen of your program, eg.
    PARAMETERS:
     p_idoc type edidc-docnum OBLIGATORY,
     p_date type sy-datum OBLIGATORY,
     p_time type sy-uzeit OBLIGATORY.
  4. Go to your report variants and create new variant (in our case we name it ‘USE_VARS’)
  5. For each of our parameters select a “Selection variable type” and “Name of Variable”
    Assign variable from TVARVC
    Assigned variables
  6. For IDOC there is only type ‘T’ (variable from TVARVC table) available.
  7. For DATE (parameter of type date) there are also another type options
    • ‘D’ – Dynamic date calculation (Local Time)
    • ‘X’ – Dynamic date calculation (System Time)

    …with available values

      • Current date
      • Current date +/- ??? days
      • Current date +/- ??? work days
      • First day of current month
      • Nth working day of current month
      • First day of next month
      • First day of previous month
      • Last day of previous month
      • Last day of the Current month

    Dynamic variables of type DATE

  8. For TIME (parameter of type time) there are also another type options
    • ‘Z’ – Dynamic time calculation (Local Time)
    • ‘Y’ – Dynamic time calculation (System Time)

    …with available values

    • Current time
    • Current time +/- ???

    Dynamic variables of type TIME

Now we have our program variant ready so let’s try to execute our report with our variant.

Start program with variant

Selection screen before variables are modified

If we now modify the values either in program or manually via TCode SM30 in view TVARV and execute the report again with our variant ‘USE_VARS’, the parameters in selection screen will take the new/updated values from TVARVC automatically.

DATA:
  ls_tvarvc TYPE tvarvc.

SELECT SINGLE *
  INTO ls_tvarvc
  FROM tvarvc
  WHERE name = 'Z_MY_PROGRAM_LAST_IDOC'.

CHECK sy-subrc = 0.

ls_tvarvc-low = ls_tvarvc-low + 5.
MODIFY tvarvc FROM ls_tvarvc.

SELECT SINGLE *
  INTO ls_tvarvc
  FROM tvarvc
  WHERE name = 'Z_MY_PROGRAM_LAST_DATE'.

CHECK sy-subrc = 0.

ls_tvarvc-low = sy-datum.
MODIFY tvarvc FROM ls_tvarvc.

SELECT SINGLE *
  INTO ls_tvarvc
  FROM tvarvc
  WHERE name = 'Z_MY_PROGRAM_LAST_TIME'.

CHECK sy-subrc = 0.

ls_tvarvc-low = sy-uzeit.
MODIFY tvarvc FROM ls_tvarvc.

Selection screen after variables are modified

One thought on “ABAP – Using dynamic variables in program selection screen variants

  1. Hi,

    What is the difference between – Type D and X (see below)

    For DATE (parameter of type date) there are also another type options

    ‘D’ – Dynamic date calculation (Local Time)
    ‘X’ – Dynamic date calculation (System Time)

Leave a Reply to BHASKAR PALCancel reply