Skip to Content

Summary:

This article demonstrates on how to create selection screen variant for module pool programs. Variants are saved selection criteria usually for reports, which you define. You can define a variant for any module pool program in the system using the module pool selection screen.

Author(s): Dhanalakshmi M S

Company: Cognizant Technology Solutions, Chennai, India

Created on:  23 April 2012

Author Bio:

Dhanalakshmi is an ABAP Consultant. Currently working with Cognizant Technology Solutions. She has 6 years of experience in SAP ABAP. She is also interested in working with Enterprise Portal, Content Development & Customization using Web Dynpro for Java, and ABAP.

Introduction

Selection screen variants can be stored only in Reports. This document explains on how to create Selection screen variant in a module program.

Preview of Output

/wp-content/uploads/2012/04/1_95311.jpg

Merits & Demerits

Merits:

Module programming can be used with Selection Screens and similar to the report programming, variants can be saved and loaded.

Demerits:

While using Selection screen in module programming, if a field is maintained OBLIGATORY, it is impossible to navigate through the screens without filling values in the OBLIGATORY field. To avoid this, OBLIGATORY should be removed from the selection screen and a check has to be performed to see if the field is filled or not in the PAI module of the screen.

Procedure

The steps to create Screen Variants are as follows.

  • Creation of Module Pool Program with Selection screen
  • Create a screen for saving the Variants
  • Load and Display the variant
  • Create and Execute the Transaction Code

Creation of Module Pool Program with Selection Screen

Transaction Code: SE80

Program Name: SAPMZDEMO_VARIANT

/wp-content/uploads/2012/04/1_95311.jpg

Press Enter and from the appearing pop up, select With TOP INCL checkbox.

/wp-content/uploads/2012/04/1_95311.jpg

Now, the pop up asks for TOP Include name. Name the TOP Include as MZDEMO_VARIANTTOP as shown below.

/wp-content/uploads/2012/04/1_95311.jpg

From the appearing dialog box, enter as shown in the below screenshot.

/wp-content/uploads/2012/04/1_95311.jpg

Create a screen 9991 as show in the below screenshot.

/wp-content/uploads/2012/04/1_95311.jpg

/wp-content/uploads/2012/04/1_95311.jpg

Enter the screen attributes as it appear in the below screenshot.

/wp-content/uploads/2012/04/1_95311.jpg

Click on the Layout button and create a subscreen.

/wp-content/uploads/2012/04/1_95311.jpg

/wp-content/uploads/2012/04/1_95311.jpg

Name the Subscreen as ‘SUB1’ and include OK_CODE in the Element list tab.

/wp-content/uploads/2012/04/1_95311.jpg

In the TOP include MZDEMO_VARIANTTOP, write the below code:

/wp-content/uploads/2012/04/1_95311.jpg

*&———————————————————————*
*& Include           MZDEMO_VARIANTTOP

*& Module Pool       SAPMZDEMO_VARIANT

*&

*&———————————————————————*
PROGRAM SAPMZDEMO_VARIANT.
TABLES: mara.

SELECTION-SCREEN BEGIN OF SCREEN 901 AS SUBSCREEN.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text001. “Input
 
SELECTOPTIONSs_matnr  FOR  mara-matnr“Material NumberSELECTION-SCREEN SKIP.

  PARAMETERS: p_rec TYPE char4 DEFAULT ‘1000’. “Max. No. of Records

SELECTION-SCREEN END OF BLOCK b1.SELECTION-SCREEN END OF SCREEN 901.

Now double click on the screen 9991 and create GUI_STATUS for the screen 9991.

/wp-content/uploads/2012/04/1_95311.jpg

Write the below code in the screen 9991 flow logic.

PROCESS BEFORE OUTPUT.
MODULE STATUS_9991.
CALL SUBSCREEN sub1 INCLUDING syrepid ‘901’.
PROCESS AFTER
INPUT.
CALL SUBSCREEN sub1.
MODULE USER_COMMAND_9991.

In the above code,

CALL SUBSCREEN sub1 INCLUDING sy-repid ‘901’ statement is to call the Selection screen 901 (which is included in the TOP module) into the subscreen area SUB1.

Double click on the module STATUS_9991.

/wp-content/uploads/2012/04/1_95311.jpg

Click on ‘Yes’ from the appearing dialog and create the STATUS_9991 in SAPMZDEMO_VARIANT program.

/wp-content/uploads/2012/04/1_95311.jpg

Set the Icon for Execute button as shown in the above screenshot.

Perform the same operation for the button VARIANT.

/wp-content/uploads/2012/04/1_95311.jpg

And also set the icon for Variant button as ‘ICON_VARIANTS’.

Set the Function keys.

/wp-content/uploads/2012/04/1_95311.jpg

Enable the following Standard tool bar buttons.

/wp-content/uploads/2012/04/1_95311.jpg

Activate the GUI_STATUS for Screen 9991.

Double Click on the USER_COMMAND_9991 and create the PAI module.

/wp-content/uploads/2012/04/1_95311.jpg

In the appearing Module USER_COMMAND_9991 input, write the below code to navigate through the Variant Screen.

/wp-content/uploads/2012/04/1_95311.jpg

Create a screen for saving the Variants

In the above code when ‘SAVE’ button is pressed a screen should appear to get the inputs for saving the variant.

For this to work, Screen 9992 is created with selection screen to store the name of the screen variant.

Now declare the selection screen variables in TOP include as shown below.

*&———————————————————————*
*& Include MZDEMO_VARIANTTOP

*& Module Pool  SAPMZDEMO_VARIANT

*&

*&———————————————————————*
PROGRAM  SAPMZDEMO_VARIANT.
TABLES: mara.

DATA: ok_code     TYPE syucomm,
      g_variant1 
TYPE rsvarvariant,
      g_user_vari
TYPE rsvarvariant,
      g_vari_report
TYPE rsvarreport,
      g_sel_var    
TYPE rsvarvariant,
      g_sel_vartxt 
TYPE rsvarvtext,
      g_report     
TYPE rsvarreport,
      g_varexists  
TYPE c.

SELECTION-SCREEN BEGIN OF SCREEN 901 AS SUBSCREEN.

     SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text001. “Input
     
SELECTOPTIONS: s_matnr  FOR  maramatnr.                    “Material NumberSELECTION-SCREEN SKIP.

      PARAMETERS: p_rec  TYPE char4 DEFAULT ‘1000’ OBLIGATORY. “Max. No. of Records

          SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN END OF SCREEN 901.


SELECTION-SCREEN BEGIN OF SCREEN 902 AS SUBSCREEN.

     SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text001.

       PARAMETERS: p_name TYPE rsvarvariant,
                   p_mean
TYPE rsvarvtext.

     SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN END OF SCREEN 902.

/wp-content/uploads/2012/04/1_95311.jpg

P_NAME in the selection screen of block 902 represents Variant Name and P_MEAN represents the description / meaning of the selection screen variant (As it appears in the Report variant screen).

/wp-content/uploads/2012/04/1_95311.jpg

Click on the Layout button of screen 9992 and create a Subscreen SUB2.

Include OK_CODE in the element list of Screen 9992.

/wp-content/uploads/2012/04/1_95311.jpg

In the Flow logic of Screen 9992, include the below code.


PROCESS BEFORE
OUTPUT.
 
MODULE status_9992.
 
CALL SUBSCREEN sub2 INCLUDING syrepid ‘902’.

PROCESS AFTER INPUT.
 
CALL SUBSCREEN sub2.
    MODULE user_command_9992.

As created for Screen 9991, create GUI STATUS for 9992 as ZGUI_9992.

/wp-content/uploads/2012/04/1_95311.jpg

Include this ZGUI_9992 status in PBO module of 9992 screen.

*&———————————————————————*

*&            Module  STATUS_9992  OUTPUT

*&———————————————————————*

*             Status for Screen 9992

*———————————————————————-*

module STATUS_9992 output.
 
SET PF-STATUS ‘ZGUI_9992’.

endmodule” STATUS_9992  OUTPUT

The screen to save the variant has now been created.

Create the module USER_COMMAND_9992 input by double clicking on it and paste the below code.

*&———————————————————————*

*&     Module  USER_COMMAND_9992  INPUT

*&———————————————————————*

*———————————————————————-*

module USER_COMMAND_9992 input.

IF NOT p_name IS INITIAL.

  ok_code =  ‘OK’.

ELSE.

  ok_code = ‘CANCEL’.

ENDIF.

CASE ok_code.

  WHEN ‘OK’.

  IF NOT p_name IS INITIAL.

   PERFORM save_variant.  –> See section A.1 below

   MESSAGE s001(VL) WITH texte02.  “Variant has been saved.

  ENDIF.

  WHEN ‘CANCEL’.

  MESSAGE s001(VL) WITH texte03 DISPLAY LIKE ‘E’. “Variant Not saved.

ENDCASE.

CALL SCREEN 9991.

endmodule.  ” USER_COMMAND_9992  INPUT

Section A.1:

To save the variant, RS_CREATE_VARIANT function module is used. Double click on the SAVE_VARIANT subroutine and create it under the Include MZDEMO_VARIANT_LOAD_VARIANTF01.

When Save button from Screen 9991 is clicked, a popup screen which was created for getting attributes for variant name and description should be displayed. In the appearing screen 9992 pop screen, as shown below, when OK button is pressed after filling values in the fields, function module RS_CREATE_VARIANT will push the values entered in the selection screen into the standard variant table.

/wp-content/uploads/2012/04/1_95311.jpg

Copy and paste the below code inside the SAVE_VARIANT subroutine.

*&———————————————————————*

*&  Form  SAVE_VARIANT

*&———————————————————————*

*  Routine to Save the variant

*———————————————————————-*

form SAVE_VARIANT .
 
DATA: BEGIN OF rsparams_tab OCCURS 10.
           
INCLUDE STRUCTURE rsparams.
 
DATA: END OF rsparams_tab.
 
DATA: BEGIN OF varid_tab.
 
INCLUDE STRUCTURE varid.
 
DATA: END OF varid_tab.
 
DATA: BEGIN OF varit_tab OCCURS 2.
           
INCLUDE STRUCTURE varit.
 
DATA: END OF varit_tab.
 
DATA: rc  TYPE systsubrc.
* Tabellen initialisieren
 
CLEAR: varid_tab.
 
REFRESH varit_tab.
 
REFRESH rsparams_tab.
*fill VARID structure – variant description
  varid_tab
report  = ‘SAPMZDEMO_VARIANT’.
  varid_tab
variant = p_name.
  varid_tab
environmnt   = ‘A’.
  varit_tab
mandt   = symandt.
  varit_tab
langu   = sylangu.
  varit_tab
report  = varid_tabreport.
  varit_tab
variant = varid_tabvariant.
  varit_tab
vtext   = p_mean.
 
APPEND varit_tab.
*fill RSPARAMS structure – Selektionswerte; variant contents
 
LOOP AT s_matnr.
   rsparams_tab
selname = ‘S_MATNR’.
   rsparams_tab
kind    = ‘S’.
   rsparams_tab
sign    = s_matnrsign.
   rsparams_tab
option  = s_matnroption.
   rsparams_tab
low     = s_matnrlow.
   rsparams_tab
high    = s_matnrhigh.
  
APPEND rsparams_tab.
  
ENDLOOP.

  rsparams_tabselname = ‘P_REC’.
  rsparams_tab
kind  = ‘P’.
  rsparams_tab
sign  = ‘I’.
  rsparams_tab
option  = ‘EQ’.
  rsparams_tab
low  = p_rec.
  rsparams_tab
high  = ‘ ‘.
 
APPEND rsparams_tab.
*Check variant
 
CALL FUNCTION ‘RS_VARIANT_EXISTS’
 
EXPORTING
  
report  = varid_tabreport
   variant
= varid_tabvariant
 
IMPORTING
   r_c 
= rc
 
EXCEPTIONS
   not_authorized 
= 01
   no_report 
= 02
   report_not_existent
= 03
   report_not_supplied
= 04.

IF sysubrc <> 0.
 
MESSAGE e001(VL) WITH texte22 DISPLAY LIKE ‘S’.
ENDIF.

  IF rc = 0.                    ” Variante existiert

    CALL FUNCTION ‘RS_CHANGE_CREATED_VARIANT’
     
EXPORTING
       curr_report     
= varid_tabreport
       curr_variant    
= varid_tabvariant
       vari_desc       
= varid_tab
     
TABLES
       vari_contents   
= rsparams_tab
       vari_text       
= varit_tab
     
EXCEPTIONS
       illegal_report_or_variant
= 01
       illegal_variantname      
= 02
       not_authorized           
= 03
       not_executed             
= 04
       report_not_existent      
= 05
       report_not_supplied      
= 06
       variant_doesnt_exist     
= 07
       variant_locked           
= 08
       selections_no_match      
= 09.
 
ELSE.
    
CALL FUNCTION ‘RS_CREATE_VARIANT’    

     EXPORTING
          curr_report  
= varid_tabreport
          curr_variant 
= varid_tabvariant
          vari_desc    
= varid_tab
     TABLES
          vari_contents  
= rsparams_tab
          vari_text      
= varit_tab    
     EXCEPTIONS
          illegal_report_or_variant
= 01
          illegal_variantname  
= 02
          not_authorized       
= 03
          not_executed         
= 04
          report_not_existent  
= 05
          report_not_supplied  
= 06
          variant_exists       
= 07
          variant_locked       
= 08.
ENDIF.
  rc
= sysubrc.endform.           ” SAVE_VARIANT

Load and Display the variants

In order to choose and display the variants, when Variant button in 9991 screen is pressed, RS_SUPPORT_SELECTIONS and RS_VARIANT_CATALOG function modules have to be used respectively.

*&———————————————————————*

*& Module  USER_COMMAND_9991  INPUT

*&———————————————————————*

*  text

*———————————————————————-*

module USER_COMMAND_9991 input.
     CASE ok_code.    

       WHEN ‘SAVE’.    
          CLEAR: p_name, p_mean.    
          CALL SCREEN 9992 STARTING AT 10 5.

      WHEN ‘VARIANT’.
           PERFORM load_variant.  –>  See Section A.2 below    
           IF syucomm EQ ‘CANC’.    
               MESSAGE s001(VL) WITH texte01 DISPLAY LIKE ‘E’“Variant operation terminated    
           ELSE.
              MESSAGE s001(VL) WITH texts01.                                 “Variant selected
    
ENDIF.    
          CALL SCREEN 9991.
 
WHEN OTHERS.
    
LEAVE TO SCREEN 9991.    
    ENDCASE.

ENDMODULE.                 ” USER_COMMAND_9991  INPUT

/wp-content/uploads/2012/04/1_95311.jpg

Section A.2:

When SAVE button from Screen 9991 is clicked, the variant for the screen should get displayed. For this to work, Write the below code under PERFORM LOAD_VARIANT subroutine.

Double click on LOAD_VARIANT routine, a Pop will appear, asking for creating the LOAD_VARIANT subroutine. Create this routine in an include MZDEMO_VARIANT_LOAD_VARIANTF01.

This include will automatically inserted into the Main program SAPMZDEMO_VARIANT.

*&———————————————————————*

*&      Form  LOAD_VARIANT

*&———————————————————————*

*———————————————————————-*

form LOAD_VARIANT . 

   PERFORM choose_variant CHANGING g_sel_var.  –> See section A.3

     IF g_sel_var NE space.

       CALL FUNCTION ‘RS_SUPPORT_SELECTIONS’

          EXPORTING

          report      =         syrepid
           variant     =        
g_sel_var
     
EXCEPTIONS

             variant_not_existent = 1
             variant_obsolete    
= 2
            
OTHERS               = 3.

            ENDIF.

endform.                    ” LOAD_VARIANT

This RS_SUPPORT_SELECTIONS function module is used to load all the variants created for the program SAPMZDEMO_VARIANT.

Section A.3:

From the appearing Variant dialog box, when the user selects a variant from the list, the values in the variant should get copied to the fields of the selection screen. Hence, create a subroutine CHOOSE_VARIANT and write the below code.

*&———————————————————————*

*&      Form  CHOOSE_VARIANT

*&———————————————————————*

*       text

*———————————————————————-*

*      <–FP_SEL_VAR  text

*———————————————————————-*

form CHOOSE_VARIANT  changing fp_sel_var.
 
CALL FUNCTION ‘RS_VARIANT_CATALOG’
   
EXPORTING
     
report               = syrepid
      masked              
= ‘X’
   
IMPORTING
      sel_variant         
= fp_sel_var
      sel_variant_text    
= g_sel_vartxt
   
EXCEPTIONS
      no_report           
= 1
      report_not_existent 
= 2
      report_not_supplied 
= 3
      no_variants         
= 4
      no_variant_selected 
= 5
      variant_not_existent
= 6
     
OTHERS               = 7.

endform.                    ” CHOOSE_VARIANT

Create and Execute the Transaction Code

Activate the entire program and create a Transaction code ZSCR_VAR by right clicking on the Object name from Left pane of the window as shown below.

/wp-content/uploads/2012/04/1_95311.jpg

From the appearing dialog box, pass on the values as shown below.

/wp-content/uploads/2012/04/1_95311.jpg

As shown in the below screenshot, maintain the attributes of the transaction code.

/wp-content/uploads/2012/04/1_95311.jpg

Save it. Execute the transaction code ZSCR_VAR.

/wp-content/uploads/2012/04/1_95311.jpg

Provide some values in the Material and Max. No. of Records field in the selection screen and click on save.

/wp-content/uploads/2012/04/1_95311.jpg

The variant DEMO_VAR is saved and the following message will get displayed.

/wp-content/uploads/2012/04/1_95311.jpg

Click on the Variant button . /wp-content/uploads/2012/04/1_95311.jpg

From the appearing dialog, select the saved variant.

To report this post you need to login first.

12 Comments

You must be Logged on to comment or reply to a post.

  1. Sijin Chandran

    Very thanks for this Informative DOC.

    Was searching for this since long , need not to say Maricela’s comment made me notice this , thanks to Maricela as well 🙂

    Will give this a try for sure .

    (0) 
  2. Naresh VVSR

    Hello Dhana Lakshmi,

    I read through this page, it was quite a useful information. But in addition to this we also have a use case where we need the variant along with the attributes in it.

    In short we need the look and feel of selection variant screen along with all attributes in the module pool program selection,  which appear in a normal report.

    Do you have the steps from which can help to achieve this as well. As an initial analysis, i see that FG: SVAR has many Function modules, but not sure how to use these.

    Below is screen that we need to have the module pool program.

    Selection Variant attributes.PNG

    (0) 
    1. Raymond Giuseppi

      You could, but use following FM

      • RS_VARIANT_SAVE_FROM_SELSCREEN to save current screen parameters to variant
      • RS_VARIANT_FETCH to update screen from variant

      Sample (PAI user_command)

      when ‘SPOS’.
      ls_report = sy-repid.
      clear lt_dynnr.
      refresh lt_dynnr[].
      append ‘0400’ to lt_dynnr[].
      export lt_dynnr to memory id ‘%_SCRDYN_%’.
      sy-dynnr = ‘0400’. ” selection-screen dynpro
      sy-slset = gv_selset_0300. ” global value to store current variant (like sy-slset)
      call function ‘RS_VARIANT_SAVE_FROM_SELSCREEN’
      exporting
        curr_report          = ls_report
        user_vari            = gv_selset_0300
        vari_report          = ls_report
      importing
        variant = gv_selset_0300
      exceptions
        illegal_variant_name = 0
        not_authorized       = 0
        no_report            = 0
        report_not_existent  = 0
        report_not_supplied  = 0
        others = 0.
      sy-dynnr = ‘0300’.
      when ‘GET’.
      ls_report = sy-repid.
      ls_key-report = sy-repid.
      ls_key-variant = gv_selset_0300.
      sy-dynnr = ‘0400’. ” selection subscreen
      sy-slset = gv_selset_0300.
      call function ‘RS_VARIANT_FETCH’
      exporting
        function = ‘GET’
        report = ls_report
        rkey = ls_key
        submode = space
        dynnr = ‘0400’
      importing
        variant = gv_selset_0300
      tables
        selctab = lt_sscr
      exceptions
        variant_name         = 0
        variant_inconsistent = 0
        only_background      = 0.
      sy-dynnr = ‘0300’. ” main screen container of selection subscreen
      when ‘VARI’.
      ls_report = sy-repid.
      call function ‘RS_VARIANT_LIST’
      exporting
        report = ls_report
      exceptions
        no_report           = 0
        no_variants         = 0
      report_not_existent = 0
        report_not_supplied = 0
        others = 0.

      Regards,

      Raymond

      (0) 
  3. Abhilash Pradhan

    Hello,

    The demerit written is not exactly true, I have found a way to avoid it and i am sharing that.

    Demerits:

    While using Selection screen in module programming, if a field is maintained OBLIGATORY, it is impossible to navigate through the screens without filling values in the OBLIGATORY field. To avoid this, OBLIGATORY should be removed from the selection screen and a check has to be performed to see if the field is filled or not in the PAI module of the screen.

    WORKAROUND

    Create the Function Code for the Choose Variant Field with the Function type EXIT Command.

    If your PAI , in the module AT EXIT Command, check for OK_CODE = VARIANT and then call the LOAD_VARIANT subroutine.

    Now use LEAVE TO SCREEN OR CALL SCREEN Command to trigger the PBO of the calling screen and VOILA you wont trigger any OBLIGATORY Checks now.

    You can then proceed with your execution as required.

    Please like if you found it useful.

    (0) 

Leave a Reply