Friday, 16 September 2016

Creation of Selection Screen Variant in Module Pool Programming

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


Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

Create a screen 9991 as show in the below screenshot.

Creation of Selection Screen Variant in Module Pool Programming

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

Click on the Layout button and create a subscreen.


Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

In the TOP include MZDEMO_VARIANTTOP, write the below code:

Creation of Selection Screen Variant in Module Pool Programming

*&---------------------------------------------------------------------*
*& 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 text-001. "Input
  SELECT-OPTIONS:  s_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.

Creation of Selection Screen Variant in Module Pool Programming

Write the below code in the screen 9991 flow logic.

PROCESS BEFORE OUTPUT.
MODULE STATUS_9991.
CALL SUBSCREEN sub1 INCLUDING sy-repid '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.

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

Set the Icon for Execute button as shown in the above screenshot.
Perform the same operation for the button VARIANT.

Creation of Selection Screen Variant in Module Pool Programming

And also set the icon for Variant button as 'ICON_VARIANTS'.
Set the Function keys

Creation of Selection Screen Variant in Module Pool Programming

Enable the following Standard tool bar buttons.

Creation of Selection Screen Variant in Module Pool Programming

Activate the GUI_STATUS for Screen 9991.
Double Click on the USER_COMMAND_9991 and create the PAI module.

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

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 sy-ucomm,
      g_variant1  TYPE rsvar-variant,
      g_user_vari TYPE rsvar-variant,
      g_vari_report TYPE rsvar-report,
      g_sel_var     TYPE rsvar-variant,
      g_sel_vartxt  TYPE rsvar-vtext,
      g_report      TYPE rsvar-report,
      g_varexists   TYPE c.

SELECTION-SCREEN BEGIN OF SCREEN 901 AS SUBSCREEN.
     SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. "Input
      SELECT-OPTIONS: s_matnr  FOR  mara-matnr.                    "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 text-001.
       PARAMETERS: p_name TYPE rsvar-variant,
                   p_mean TYPE rsvar-vtext.
     SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN END OF SCREEN 902.

Creation of Selection Screen Variant in Module Pool Programming

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).

Creation of Selection Screen Variant in Module Pool Programming

Click on the Layout button of screen 9992 and create a Subscreen SUB2.
Include OK_CODE in the element list of Screen 9992.

Creation of Selection Screen Variant in Module Pool Programming

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

PROCESS BEFORE OUTPUT.
  MODULE status_9992.
  CALL SUBSCREEN sub2 INCLUDING sy-repid '902'.

PROCESS AFTER INPUT.
  CALL SUBSCREEN sub2.
    MODULE user_command_9992.

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

Creation of Selection Screen Variant in Module Pool Programming

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 text-e02.  "Variant has been saved.
  ENDIF.
  WHEN 'CANCEL'.
  MESSAGE s001(VL) WITH text-e03 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.

Creation of Selection Screen Variant in Module Pool Programming

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 syst-subrc.
* 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   = sy-mandt.
  varit_tab-langu   = sy-langu.
  varit_tab-report  = varid_tab-report.
  varit_tab-variant = varid_tab-variant.
  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_matnr-sign.
   rsparams_tab-option  = s_matnr-option.
   rsparams_tab-low     = s_matnr-low.
   rsparams_tab-high    = s_matnr-high.
   APPEND rsparams_tab.
   ENDLOOP.

  rsparams_tab-selname = '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_tab-report
   variant = varid_tab-variant
  IMPORTING
   r_c  = rc
  EXCEPTIONS
   not_authorized  = 01
   no_report  = 02
   report_not_existent = 03
   report_not_supplied = 04.

IF sy-subrc <> 0.
  MESSAGE e001(VL) WITH text-e22 DISPLAY LIKE 'S'.
ENDIF.

  IF rc = 0.                    " Variante existiert
    CALL FUNCTION 'RS_CHANGE_CREATED_VARIANT'
      EXPORTING
       curr_report      = varid_tab-report
       curr_variant     = varid_tab-variant
       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_tab-report
          curr_variant  = varid_tab-variant
          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 = sy-subrc.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 sy-ucomm EQ 'CANC'.     
               MESSAGE s001(VL) WITH text-e01 DISPLAY LIKE 'E'.  "Variant operation terminated     
           ELSE.
              MESSAGE s001(VL) WITH text-s01.                                 "Variant selected
     ENDIF.     
          CALL SCREEN 9991.
  WHEN OTHERS.
     LEAVE TO SCREEN 9991.     
    ENDCASE.
ENDMODULE.                 " USER_COMMAND_9991  INPUT

Creation of Selection Screen Variant in Module Pool Programming

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      =         sy-repid
           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               = sy-repid
      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.

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

Save it. Execute the transaction code ZSCR_VAR.

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

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

Creation of Selection Screen Variant in Module Pool Programming

Click on the Variant button   Creation of Selection Screen Variant in Module Pool Programming From the appearing dialog, select the saved variant.

No comments:

Post a Comment