Standard service - a fixed amount of work that is performed at a fixed price.  Post Service

  

Thursday, 04 April 2019 20:15

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

Written by 
Rate this item
(0 votes)

No HANA, no UI5 no OData in this article. This is a basic ABAP utility code snippet

to create variant of a program if it does not exist or update an existing variant from a program. This is a very general requirement in SAP projects. When one of our team members got this requirement, he thought of sharing the whole reusable snippet here so that everyone can leverage it in their future projects.

This is a simple selection screen in the report for which we need to create/update the variant from another program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<code>*&amp;---------------------------------------------------------------------*
*&amp; Report ZMAIN_PROGRAM
*&amp; Just the Selection Screen
*&amp;---------------------------------------------------------------------*
REPORTZMAIN_PROGRAMNOSTANDARDPAGEHEADING
LINE-COUNT132.
*---------------------------------------------------------------------*
* TABLES *
*---------------------------------------------------------------------*
TABLES:erdk.
*---------------------------------------------------------------------*
* SELECTION SCREEN *
*---------------------------------------------------------------------*
SELECTION-SCREENBEGIN OF BLOCKb1WITHFRAMETITLEtext-001.
SELECT-OPTIONS:
* Select Options in the selection screen
s_partFORerdk-partner," Customer
s_accFORerdk-vkont." Account
PARAMETERS:
p_dateTYPEsy-datumOBLIGATORYDEFAULTsy-datum.
SELECTION-SCREENEND OF BLOCKb1.
SELECTION-SCREENBEGIN OF BLOCKb2WITHFRAMETITLEtext-003.
PARAMETERS:
r_finRADIOBUTTON GROUPbkcyDEFAULT'X'USER-COMMANDjaiabap,
r_ratRADIOBUTTON GROUPbkcy,
r_adhRADIOBUTTON GROUPbkcy,
r_othersRADIOBUTTON GROUPbkcy.
SELECTION-SCREENEND OF BLOCKb2.
SELECTION-SCREENBEGIN OF BLOCKb3WITHFRAMETITLEtext-004.
PARAMETERS:
r_testRADIOBUTTON GROUPwoffDEFAULT'X'USER-COMMANDhailabap,
r_comRADIOBUTTON GROUPwoff.
SELECTION-SCREENEND OF BLOCKb3.
</code>

Also Read:Trick to adjust the variants

The use case is to update the variant of program ZMAIN_PROGRAM using another program say ZCALLING_PROGRAM.

1
2
3
4
5
6
7
8
9
10
11
12
13
<code>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.</code>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code>*FilltheSelectOptions
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.
APPENDlwa_var_contentTOit_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
APPENDlwa_var_contentTOit_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'.
APPENDlwa_var_contentTOit_variant_tab.</code>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<code>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.
APPENDlwa_varitTOit_varit.</code>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code>*CreateVariant
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.
IFsy-subrcEQ0.
WRITE:/'Variant ',gv_variant_name,' created and saved successfully'COLORCOL_POSITIVE.
ELSE.
WRITE:/'Error while createing variant ',gv_variant_nameCOLORCOL_NEGATIVE.
ENDIF.</code>

Also Check:Just 4 Versions of the same program to understand OOPs ABAP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<code>*ChangeVariant
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.
IFsy-subrcEQ0.
WRITE:/'Variant ',gv_variant_name,' updated successfully'COLORCOL_POSITIVE.
ELSE.
WRITE:/'Error while updating variant ',gv_variant_nameCOLORCOL_NEGATIVE.
ENDIF.</code>

Execute the second program.

The variant and description is correctly created.

The variant description is Test Variant and the Account Number has 100, 200, and 300.

Now let’s run the second program again.

It says, the variant is Updated.

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

Description is updated correctly.

The Select Options values have been updated correctly.

In case you are wondering, where can you find the details of any variant. VARID is the table where all variants are stored. Also, you can use RS_VARIANT_CONTENT function module to read the values of any variant.

You may download the full code snipped for second program from HERE.

This was a simple post for ABAP beginners who need some help in this requirement. Please browse SAPYard.com for OData Free Training, New syntaxes in SAP ABAP and some new topics like Core Data Services.

We have a very active Telegram (App) SAP Technical Group with more than 4540+ SAP Technical Practitioners from 6 Continents of the SAP World. Please join it using below link. Not a single spam message in this group. Just SAP discussion.
Telegram SAP Technical Discuss Group.
You need to install the Telegram App first on your mobile device. Once you have it on your mobile, you can join the group and also access it from the Web on your computer and laptop.

Free SAP ABAP for HANA Training Tutorials Exercises.

Read 65 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.