Dynamic variant of a ABAP Program

It is sometimes very useful to change the selection of an ABAP program dynamically. In such way you can start a program with specific parameters depending on the current conditions. Consider a SAP standard program 'A', which can deal with some objects, e.g. invoices. It has a selection screen with invoice number and you want to process invoices with it, which were imported into the system through some interface. On the selection screen you can only specify '*' or '00000000'-'99999999' but this selection will not improve your performance; the program 'A' must do a full table scan over the invoices. It would be nice to influence the variant of the program 'A' before it'll be start in the background. If you know, which invoices exactly should be processed, (because you've just imported them through your interface or you can select them from database) you can change the variant of the selection screen of the program 'A' with small program 'B' and specify this concrete invoice numbers. The program 'B' will deal as a preprocessor of the program 'A'.
Here the complete example.
Components:
  • Program ZKM_TEST with variant DEFAULT, which parameters and select-options should be changed dynamically.
  • Program ZKM_TEST2, which sets the values of the variables, which are assigned to parameters and select-options of the variant of the program ZKM_TEST.
  • The variables are stored in the table TVARVC and can be managed with transaction STVARV.
Here the step by step how implement such scenario:
1) Create the variables through the transaction STVARV (it's very straightforward, see below) or by starting the program ZKM_TEST2. After that you can find the entries for variables in the table TVARVC.
screenshot
2) Assign the variables to parameters and select-options of the variant DEFAULT of the program ZKM_TEST. Use transaction SE38, program ZKM_TEST, radio button 'Variants' and button 'Create'.
screenshot
Input 'DEFAULT' as name of variant, then select radio button 'Attributes' and button 'Create'
screenshot
Assign variables, first use 'T' as selection variable, then name of the variable.
screenshot
3) Schedule a job with two sequential steps: Program ZKM_TEST2 and ZKM_TEST (with 'DEFAULT' variant), the program ZKM_TEST2 deletes previous and stores new values of variables, the program ZKM_TEST use them as parameters and Select-Options.
REPORT zkm_test.

DATA:
  gv_bukrs TYPE bukrs.

PARAMETERS:
  pa_blart TYPE blart.

SELECT-OPTIONS:
  so_bukrs FOR gv_bukrs.

START-OF-SELECTION.

  WRITE: / 'Parameter PA_BLART:', pa_blart.

  SKIP.

  LOOP AT so_bukrs.
    WRITE: / 'Select-Option SO_BUKRS:', so_bukrs-sign, so_bukrs-option,
                                        so_bukrs-low,  so_bukrs-high.
  ENDLOOP.
REPORT zkm_test2.

DATA:
  ls_tvarvc TYPE tvarvc,
  lt_tvarvc TYPE TABLE OF tvarvc.

DEFINE append_lt_tvarvc.
  ls_tvarvc-name = &1.
  ls_tvarvc-type = &2.
  ls_tvarvc-numb = &3.
  ls_tvarvc-sign = 'I'.
  ls_tvarvc-opti = 'EQ'.
  ls_tvarvc-low  = &4.
  append ls_tvarvc to lt_tvarvc.
END-OF-DEFINITION.

append_lt_tvarvc 'ZZZ_BLART' 'P' '0000' 'XX'.
append_lt_tvarvc 'ZZZ_BUKRS' 'S' '0000' 'A001'.
append_lt_tvarvc 'ZZZ_BUKRS' 'S' '0001' 'A002'.
append_lt_tvarvc 'ZZZ_BUKRS' 'S' '0002' 'A003'.

SORT lt_tvarvc BY name type numb.

LOOP AT lt_tvarvc INTO ls_tvarvc.

  AT NEW type.
    DELETE FROM tvarvc WHERE name = ls_tvarvc-name
                         AND type = ls_tvarvc-type.

    SKIP.
    WRITE: / 'Delete Variable', ls_tvarvc-name, ls_tvarvc-type.
  ENDAT.

  INSERT into tvarvc values ls_tvarvc.
  WRITE: / 'Insert Variable', ls_tvarvc-name, ls_tvarvc-type, 
                              ls_tvarvc-numb, ls_tvarvc-low.
ENDLOOP.
Full list of examples in my infodepot

If you have a question, have found an error or just want to contact me, please use this form.

Copyright (C) 2015 http://www.kerum.pl/infodepot/

Disclaimer: I am not affiliated or related to any division or subsidiary of SAP AG.
Trademarks or registered trademarks of any products or companies referred to on this site belong to those companies.
Anyone using the given solutions, is doing it under his/her own responsibility and at own risk.