The below program demonstrate how to change the content of variant at run time.
REPORT ztestautoselectoption.
*&--------------------------------------------------------------------&*
*& Program Description: &*
*& ----------------------- &*
*& This demo program shows how to dynamically populate the data in a &*
*& variant. &*
*& &*
*& Author: ABAPCOOKBOOK &*
*& Website: www.abapcookbook.com &*
************************************************************************
************************************************************************
* DATA DECLARATIONS *
************************************************************************
*Tables:
TABLES:
vbap.
*Internal tables:
DATA:
gt_vbap TYPE STANDARD TABLE OF vbap,
gt_vari TYPE STANDARD TABLE OF rsparams.
*Field Symbols:
FIELD-SYMBOLS:
<fs_vbap> TYPE vbap.
*Structures:
DATA:
gst_vari TYPE rsparams,
gst_varidesc TYPE varid.
*Variables:
DATA:
gv_subrc TYPE sy-subrc.
*Constants:
CONSTANTS:
gc_write TYPE c VALUE \'W\',
gc_selmat TYPE rsparams-selname VALUE \'S_MATNR\',
gc_kind TYPE rsparams-kind VALUE \'S\',
gc_sign TYPE rsparams-sign VALUE \'I\',
gc_option TYPE rsparams-low VALUE \'EQ\'.
************************************************************************
* SELECTION SCREEN *
************************************************************************
SELECT-OPTIONS:
* Material Number.
s_matnr FOR vbap-matnr OBLIGATORY.
************************************************************************
* CODE LOGIC *
************************************************************************
*Check if the variant used to run the program exists.
CALL FUNCTION \'RS_VARIANT_EXISTS\'
EXPORTING
report = sy-repid \"Program Name
variant = sy-slset \"Variant Name
IMPORTING
r_c = gv_subrc
EXCEPTIONS
not_authorized = 01
no_report = 02
report_not_existent = 03
report_not_supplied = 04.
IF gv_subrc EQ 0.
* Select sales order data from table VBAP.
SELECT * UP TO 10 ROWS
FROM vbap
INTO TABLE gt_vbap.
IF sy-subrc EQ 0.
* Retrieve the contents of the variant to the internal table \'GT_VARI\'.
CALL FUNCTION \'RS_VARIANT_CONTENTS\'
EXPORTING
report = sy-repid \"Program Name
variant = sy-slset \"Variant Name
move_or_write = gc_write
TABLES
valutab = gt_vari \"Variant Contents
EXCEPTIONS
variant_non_existent = 1
variant_obsolete = 2
OTHERS = 3.
IF sy-subrc EQ 0.
* Delete the current content of select option \'S_MATNR\' from the variant. This is
* done by deleting all \'S_MATNR\' selname entries from material select option(GT_VARI).
* We will then populate it with fresh new data later on.
DELETE gt_vari WHERE selname EQ gc_selmat.
* Building up and assigning new material numbers to
* the table variant(GT_VARI).
LOOP AT gt_vbap ASSIGNING <fs_vbap>.
* Assigning the necessary values and parameters
* to the select options via the table LT_VARI.
gst_vari-selname = gc_selmat.
gst_vari-kind = gc_kind.
gst_vari-option = gc_option.
gst_vari-sign = gc_sign.
gst_vari-low = <fs_vbap>-matnr.
* Appending the material number to the select option.
APPEND gst_vari TO gt_vari.
ENDLOOP.
* Re-populating the variant description.
gst_varidesc-mandt = sy-mandt.
gst_varidesc-aename = sy-uname.
gst_varidesc-aedat = sy-datum.
gst_varidesc-aetime = sy-uzeit.
* Creating a new variant with the new values.
CALL FUNCTION \'RS_CHANGE_CREATED_VARIANT\'
EXPORTING
curr_report = sy-repid \"Program Name
curr_variant = sy-slset \"Variant Name
vari_desc = gst_varidesc \"Variant Description
TABLES
vari_contents = gt_vari \"Variant Contents
EXCEPTIONS
illegal_report_or_variant = 1
illegal_variantname = 2
not_authorized = 3
not_executed = 4
report_not_existent = 5
report_not_supplied = 6
variant_doesnt_exist = 7
variant_locked = 8
selections_no_match = 9
OTHERS = 10.
IF sy-subrc EQ 0.
* Variant has been populated with new data successfully.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
Please note that to test the above program, create a variant with dummy data.
Then, just select the variant, the program will automatically change the content of the select option \’S_MATNR\’ based on the data retrieved from VBAP-MATNR.