Changing variants values programmatically(ABAP)

Hello ABAPers,

This is my very first Blog on ABAP Development and I am thinking sharing my experience with you will definitely help someone.

Please follow this blog for more contents on ABAP.

Requirement: Based on some condition in Program1, change the values from all the variants of Program2.

For this test scenario, I have two variants for program2 ZSAT_TEST_VARIANT.

Variant 1:  ZSAT

Variant 2: ZSAT1

For example, after executing Program1, I need to delete 4th value of all variants, i.e. D10000 and D2000 will be deleted in this case.

After execution of Program1, we can see the expected result.

You can see variant’s values D10000 and D2000 got deleted from all variants of program2.

Complete Code for your reference.

REPORT zsat_test_variant1.

DATA :  gt_cat       TYPE rsvcat,
gt_valutab   TYPE TABLE OF rsparams,
gs_vari_desc TYPE varid.

***Read all variants for program ZSAT_TEST_VARIANT

CALL FUNCTION ‘RS_ALL_VARIANTS_4_1_REPORT’
EXPORTING
program             = ‘ZSAT_TEST_VARIANT’
IMPORTING
cat                 = gt_cat
EXCEPTIONS
report_not_existent = 1
OTHERS              = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.

***DELETE 4th row of each variant value
LOOP AT gt_cat-cat INTO DATA(gw_cat).
WRITE : / gw_cat-variant.
REFRESH : gt_valutab.

CALL FUNCTION ‘RS_VARIANT_CONTENTS’
EXPORTING
report               = ‘ZSAT_TEST_VARIANT’
variant              = gw_cat-variant
move_or_write        = ‘M’
TABLES
valutab              = gt_valutab
EXCEPTIONS
variant_non_existent = 1
variant_obsolete     = 2
OTHERS               = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.
*      LOOP AT gt_valutab ASSIGNING FIELD-SYMBOL(<fs_tab>).

*You can write your own logic here, for Demo purpose I’m just deleting 4th value from all variants
DELETE gt_valutab INDEX 4.
*      ENDLOOP.

IF sy-subrc IS INITIAL.

***Update the variant
CALL FUNCTION ‘RS_CHANGE_CREATED_VARIANT’
EXPORTING
curr_report               = ‘ZSAT_TEST_VARIANT’
curr_variant              = gw_cat-variant
vari_desc                 = gs_vari_desc
TABLES
vari_contents             = gt_valutab
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 <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.

Request readers to comment in case any improvement needed or highlight any mistake from above blog.