OO ABAP – Selection Object with Direct Access

By | July 9, 2015 | ABAP Objects, OO Design Patterns | 3,146 | 6

In continuation to the previous article and in quest about the Selection object, lets check out this different flavor of the selection screen object.

Introduction

Thanks to Clemens Li for sharing this utility class in his comment on the article Object Oriented Approach for Reports with multiple Datasource

Selection criteria class, which I generally refer to as the Selection Object. If you haven’t read about where it would fit in the overall design, I suggest you read ABAP Object Oriented Approach for Reports – Redesign.

Selection Screen Object with Direct Access

This utility class is based on the global memory access using the field-symbols. I know, that you know that I don’t like the global memory access (Why NOT to use Field-Symbols to access Global Memory?). But, lets just explore this option as well.

This option, contrary to other options, this approach doesn’t need you to create any attribute in the class. That would be considered a win, if you have many select options to deal with. But, it would also defeat the purpose, if there is no selection screen variables.

Also, since it is generic, you don’t need to create the class again and again into your development. You can simply reuse this class for all reports. Just instantiate the object and done. But, One more drawback is, when you get or a set the value, you would need to create temporary tables / variables in order to receive the value back.

There are 4 methods:

  • GET_: These methods are to get the value of the select-option (GET_SELECT_OPTION_TABLE) or a Parameter (GET_PARAMETER_VALUE)
  • SET_: These methods are to set the value to the select-option (SET_SELECT_OPTION_TABLE) or a Parameter (SET_PARAMETER_VALUE)

Code lines

Utility class for Selection Criteria

 
CLASS lcl_selection DEFINITION.
  PUBLIC SECTION.
    METHODS get_parameter_value
        IMPORTING
          !iv_parameter_name TYPE feld-name
        RETURNING
          value(rv_value) TYPE string .
    METHODS get_select_option_table
      IMPORTING
        !iv_select_option TYPE feld-name
      RETURNING
        value(rt_range) TYPE rsis_t_range .
    METHODS set_parameter_value
      IMPORTING
        !iv_parameter_name TYPE feld-name
        !iv_value TYPE string .
    METHODS set_select_option_table
      IMPORTING
        !iv_select_option TYPE feld-name
        !it_range TYPE rsis_t_range .
ENDCLASS.                    "lcl_selection DEFINITION
*
*
CLASS lcl_selection IMPLEMENTATION.
 
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER->GET_PARAMETER_VALUE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_PARAMETER_NAME              TYPE        FELD-NAME
* | [<-()] RV_VALUE                       TYPE        STRING
* +-------------------------------------------------------------------------------------
  METHOD get_parameter_value.
    DATA:
      lv_assign TYPE string.
    FIELD-SYMBOLS:
      <fs>   TYPE any.
    lv_assign = '(' && sy-cprog && ')' && iv_parameter_name.
    ASSIGN (lv_assign) TO <fs>.
    ASSERT sy-subrc = 0.
    rv_value = <fs>.
  ENDMETHOD.                    "GET_PARAMETER_VALUE
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER-&gt;GET_SELECT_OPTION_TABLE
* +-------------------------------------------------------------------------------------------------+
* | [---&gt;] IV_SELECT_OPTION               TYPE        FELD-NAME
* | [&lt;-()] RT_RANGE                       TYPE        RSIS_T_RANGE
* +-------------------------------------------------------------------------------------
  METHOD get_select_option_table.
    DATA:
      lv_assign TYPE string.
    FIELD-SYMBOLS:
      <tab>     TYPE table,
      <line>    TYPE any,
      <sign>    TYPE any,
      <option>  TYPE any,
      <low>     TYPE any,
      <high>    TYPE any,
      <range>   TYPE LINE OF rsis_t_range.
    lv_assign = '(' && sy-cprog && ')' && iv_select_option && '[]'.
    ASSIGN (lv_assign) TO <tab>.
    ASSERT sy-subrc = 0.
    LOOP AT <tab> ASSIGNING <line>.
      APPEND INITIAL LINE TO rt_range ASSIGNING <range>.
      ASSIGN COMPONENT 'SIGN'   OF STRUCTURE <line>  TO <sign>.
      ASSIGN COMPONENT 'OPTION' OF STRUCTURE <line>  TO <option>.
      ASSIGN COMPONENT 'LOW'    OF STRUCTURE <line>  TO <low>.
      ASSIGN COMPONENT 'HIGH'   OF STRUCTURE <line>  TO <high>.
      <range>-sign   = <sign>.
      <range>-option = <option>.
      <range>-low    = <low>.
      <range>-high   = <high>.
    ENDLOOP.
  ENDMETHOD.                    "GET_SELECT_OPTION_TABLE
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER-&gt;SET_PARAMETER_VALUE
* +-------------------------------------------------------------------------------------------------+
* | [---&gt;] IV_PARAMETER_NAME              TYPE        FELD-NAME
* | [---&gt;] IV_VALUE                       TYPE        STRING
* +-------------------------------------------------------------------------------------
  METHOD set_parameter_value.
    DATA:
      lv_assign TYPE string.
    FIELD-SYMBOLS:
      <fs>     TYPE any.
    lv_assign = '(' && sy-cprog && ')' && iv_parameter_name.
    ASSIGN (lv_assign) TO <fs>.
    ASSERT sy-subrc = 0.
    <fs> = iv_value.
  ENDMETHOD.                    "SET_PARAMETER_VALUE
* ---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_ZJL_EIF_MENGEN_SONDER-&gt;SET_SELECT_OPTION_TABLE
* +-------------------------------------------------------------------------------------------------+
* | [---&gt;] IV_SELECT_OPTION               TYPE        FELD-NAME
* | [---&gt;] IT_RANGE                       TYPE        RSIS_T_RANGE
* +-------------------------------------------------------------------------------------
  METHOD set_select_option_table.
    DATA:
      lv_assign TYPE string.
    FIELD-SYMBOLS:
      <tab>     TYPE table,
      <range>   TYPE LINE OF rsis_t_range,
      <line>    TYPE any,
      <sign>    TYPE any,
      <option>  TYPE any,
      <low>     TYPE any,
      <high>    TYPE any,
      <range2>  TYPE LINE OF rsis_t_range.
    lv_assign = '(' && sy-cprog && ')' && iv_select_option && '[]'.
    ASSIGN (lv_assign) TO <tab>.
    ASSERT sy-subrc = 0.
    CLEAR <tab>.
    LOOP AT it_range ASSIGNING <range>.
      APPEND INITIAL LINE TO <tab> ASSIGNING <line>.
      ASSIGN COMPONENT 'SIGN'   OF STRUCTURE <line>  TO <sign>.
      ASSIGN COMPONENT 'OPTION' OF STRUCTURE <line>  TO <option>.
      ASSIGN COMPONENT 'LOW'    OF STRUCTURE <line>  TO <low>.
      ASSIGN COMPONENT 'HIGH'   OF STRUCTURE <line>  TO <high>.
      <sign>    = <range>-sign.
      <option>  = <range>-option.
      <low>     = <range>-low.
      <high>    = <range>-high.
    ENDLOOP.
  ENDMETHOD.                    "set_select_option_table
 
ENDCLASS.                    "lcl_selection IMPLEMENTATION
 

To test it out:

Test Program

 
DATA: lo_sel TYPE REF TO lcl_selection.
 
SELECT-OPTIONS: s_erdat FOR sy-datum OBLIGATORY.
 
INITIALIZATION.
  CREATE OBJECT lo_sel.
  DATA: lt_erdat_t TYPE rsis_t_range.
  DATA: ls_erdat_t LIKE LINE OF lt_erdat_t.
  DATA: lv_date TYPE d.
  ls_erdat_t = 'IEQ'.
  lv_date = sy-datum - 1.
  ls_erdat_t-low = lv_date. "sy-datum - 1.
  APPEND ls_erdat_t TO lt_erdat_t.
  lo_sel->set_select_option_table(
    iv_select_option = 'S_ERDAT'
    it_range         = lt_erdat_t
  ).
 
START-OF-SELECTION.
  "
  DATA: lt_erdat TYPE rsis_t_range.
  lt_erdat = lo_sel->get_select_option_table( 'S_ERDAT' ).
  IF sy-datum IN lt_erdat.
    WRITE: 'Date in Range' .
  ELSE.
    WRITE: 'date out of the range'.
  ENDIF.
 

Let me know your thoughts.

Credit

Clemens Li

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments

6 Comments

  • Evgen

    I use object with FM RS_REFRESH_FROM_SELECTOPTIONS to get all selscreen fields from report and then convert it to table with screen field name and PIQ_SELOPT_T table with it values. Parameters will be converted to select options.

  • Ibrahim

    the purpose, if there is no selection screen variables.
    how could be :(

    there is many selection screen variables.
    tables sflight.

    selection-screen s_test for sflight-carrid,

    if there is no table

    data lv_carrid type S_CARR_ID.

    selection-screen: s_car for lv_carrid.

    so could you please explaine me why we need this,

    If I use the above class, so i neeed to create object, and set, get parameter, fill range, etc.

    but If I use direct select-option., just one statement

    Regards
    Ibrahim

  • Ibrahim

    Hi,
    for me I use RS_REFRESH_FROM_SELECTOPTIONS and IMPORT EXPORT.

  • om prakash

    Hei Naimesh, i have a question is , how to tranfer the records from field symbol dynamic table to static internal table during run time? Please help me out! I would be very greatful for you.. Thanks and regards ,omprakash.

  • Ibrahim

    Hi prakash,
    it depends, if you know which table,
    so you do this,

    static_table = dynamich_table.

    and you can work with static table,

    loop at static_table into WA.

    endloop.

    if you don’t know which table, so you could use this ,Runtime Type Services (RTTS), but I hope Naimesh could help you.

  • Hello All,

    I know many of you love to use FM RS_REFRESH_FROM_SELECTOPTIONS for the select options .. You can find that solution at – OO ABAP – Selection Criteria Object using RS_REFRESH_FROM_SELECTOPTIONS

    Explore and let me know your different variations :)

    Thanks,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.