How to Create or Update Variant for a Report from another Program?

How to create Variants in SAP?

No HANA, no UI5 no OData in this article. This is a fundamental ABAP utility code snippet to demonstrate how to create or Update Variant for a Report from another Program if it doesn’t exist or to refresh an existing variant. This is a common requirement in SAP projects. When one of our colleagues encountered this need, he thought it beneficial to share the entire reusable piece here so everyone can utilize it in their future projects.

This is a straightforward determination screen in the report for which we really want to make/update the variation from another program.

*&---------------------------------------------------------------------*
*& Report  ZMAIN_PROGRAM
*& Just the Selection Screen
*&---------------------------------------------------------------------*
REPORT ZMAIN_PROGRAM NO STANDARD PAGE HEADING
                               LINE-COUNT 132.
*---------------------------------------------------------------------*
* TABLES                                                              *
*---------------------------------------------------------------------*
TABLES: erdk.
*---------------------------------------------------------------------*
* SELECTION SCREEN                                                    *
*---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS:
*   Select Options in the selection screen
    s_part    FOR erdk-partner,  " Customer
    s_acc    FOR erdk-vkont.    " Account
PARAMETERS:
    p_date TYPE sy-datum OBLIGATORY DEFAULT sy-datum.
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-003.
PARAMETERS:
    r_fin   RADIOBUTTON GROUP bkcy DEFAULT 'X' USER-COMMAND jaiabap,
    r_rat   RADIOBUTTON GROUP bkcy,
    r_adh   RADIOBUTTON GROUP bkcy,
    r_others RADIOBUTTON GROUP bkcy.
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-004.
PARAMETERS:
    r_test   RADIOBUTTON GROUP woff DEFAULT 'X' USER-COMMAND hailabap,
    r_com   RADIOBUTTON GROUP woff.
SELECTION-SCREEN END OF BLOCK b3.

Also Read: Trick to adjust the variants

Showing the technical names of the select options, radio button and parameters

The utilization case is to refresh the variation of program ZMAIN_PROGRAM utilizing another program say ZCALLING_PROGRAM.

Step 1 – Check if variant already exists or not using FM ‘RS_VARIANT_EXISTS’

CALL FUNCTION 'RS_VARIANT_EXISTS'
     EXPORTING
       report              = lv_program
       variant             = gv_variant_name
     IMPORTING
       r_c                 = lv_rc
     EXCEPTIONS
       not_authorized      = 1
       no_report           = 2
       report_not_existent = 3
       report_not_supplied = 4
       OTHERS              = 5.

2 – Fill variant tables with the selection screen fields

* Fill the Select Options
    lwa_var_content-selname   =  'S_PART' .
    lwa_var_content-kind      =   'S'.
    lwa_var_content-sign      =   'I'.
    lwa_var_content-option    =  'EQ' .
    lwa_var_content-low       =   lwa_cust_acc-gpartner.
    APPEND lwa_var_content TO it_variant_tab.
* Fill the Parameter
  lwa_var_content-selname   =  'P_DATE' .
  lwa_var_content-kind      =   'P'.
  lwa_var_content-sign      =   'I'.
  lwa_var_content-option    =  'EQ' .
  lwa_var_content-low       =   sy-datum. " Dummy Date. Will not be used

  APPEND lwa_var_content TO it_variant_tab.
* Fill the Radiobuttons
* Choose Others
  lwa_var_content-selname   =  'R_OTHERS' .
  lwa_var_content-kind      =   'P'.
  lwa_var_content-sign      =   'I'.
  lwa_var_content-option    =  'EQ' .
  lwa_var_content-low       =   'X'.

  APPEND lwa_var_content TO it_variant_tab.

3 – Populate the variant signatures

gs_varid-mandt        = sy-mandt.
  gs_varid-report       = lv_program.
  gs_varid-variant      = gv_variant_name. 
  gs_varid-environmnt   = 'A'.
  gs_varid-version      = '1'.
  gs_varid-ename        = sy-uname.
  gs_varid-edat         = sy-datum.
  gs_varid-etime        = sy-uzeit. 
  gs_varid-mlangu       = sy-langu.

  lwa_varit-mandt      = sy-mandt.
  lwa_varit-langu      = sy-langu.
  lwa_varit-report     = lv_program.
  lwa_varit-variant    = gv_variant_name.
  lwa_varit-vtext      = lv_text.

  APPEND lwa_varit TO it_varit.

4 – Create new variant if it does not exist using FM ‘RS_CREATE_VARIANT’

* Create Variant
   CALL FUNCTION 'RS_CREATE_VARIANT'
     EXPORTING
       curr_report               = lv_program
       curr_variant              = gv_variant_name
       vari_desc                 = gs_varid
     TABLES
       vari_contents             = it_variant_tab
       vari_text                 = it_varit
     EXCEPTIONS
       illegal_report_or_variant = 1
       illegal_variantname       = 2
       not_authorized            = 3
       not_executed              = 4
       report_not_existent       = 5
       report_not_supplied       = 6
       variant_exists            = 7
       variant_locked            = 8
       OTHERS                    = 9.
   IF sy-subrc EQ 0.
     WRITE:/ 'Variant ', gv_variant_name, ' created and saved successfully' COLOR COL_POSITIVE.
   ELSE.
     WRITE:/ 'Error while createing variant ', gv_variant_name COLOR COL_NEGATIVE.
   ENDIF.

5 – Update existing variant using FM ‘RS_CHANGE_CREATED_VARIANT’

* Change Variant
   CALL FUNCTION 'RS_CHANGE_CREATED_VARIANT'
     EXPORTING
       curr_report               = lv_program
       curr_variant              = gv_variant_name
       vari_desc                 = gs_varid
     TABLES
       vari_contents             = it_variant_tab
       vari_text                 = it_varit
     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.
     WRITE:/ 'Variant ', gv_variant_name, ' updated successfully' COLOR COL_POSITIVE.

   ELSE.
     WRITE:/ 'Error while updating variant ', gv_variant_name COLOR COL_NEGATIVE.
   ENDIF.

Let’s test it

Execute the second program.

Let’s validate

The variation and depiction is accurately made.

Update of an existing variant

The variation depiction is Test Variation and the Record Number has 100, 200, and 300.

Presently how about we run the second program once more.

According to it, the variation is Refreshed.

Also Read: Why are developers so fond of ‘REUSE_ALV_GRID_DISPLAY’?

Let’s validate again

Description is updated correctly.

The Select Choices values have been refreshed accurately.

If you are wondering where you can find the details of any variant, VARID is the table where all variants are stored. Additionally, you can use the RS_VARIANT_CONTENT function module to read the values of any variant. In this context, we will also discuss how to create or update variant for a Report from another Program, making it easier to manage your reporting needs.

You might download the full code cut for second program from HERE.

YOU MAY BE INTERESTED IN

Launch Your Career in the Cloud: A Comprehensive Guide to SAP Analytics Cloud Jobs

AI and Machine Learning in SAP: A Practical Guide