Saturday, September 24, 2016

SELECTION SCREEN and events like ONFOCUS ONBLUR for Search help?

Are you coming to ABAP from web development folks? Welcome! :) Maybe you would become a bit confused with limited set of events for ABAP Selection screens.

If you previously used to work with web form's events like onBlur, onFocus you will be here a bit disappointed. I will not promise any miracle, these events simply do not exists here at all.

But the situation how to react between form inputs is not so hopeless.

AT first there is a list of real supported events:



What we can do if we want to make a search help based on the value of previous input?

We have very limited possibilities, but luckily for this case there exists a magic workaround.

Imagine a situation on the picture below. You have a simple input for database table name. And you want to offer to user directly the list of its fields for sort. This is not possible to reach with existing events. Ok, you can call something like: "AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_sort" and then call some your nice logic with table name as a parameter. But during this call table name (previous above input) would be still empty, no table name is filled at this time, sorry :(




What to do??


You can use existing event "AT SELECTION SCREEN" event. Ok, it works partially, because it would demand some user action like hit the ENTER key or click on somewhat on the toolbar. It is not the best choice.

Or?


We can use just "AT SELECTION-SCREEN ON VALUE" but with a bit tuned coding using FM the name RS_SELECTIONSCREEN_READ. See the below code.  I removed some checks and logic to keep the coding simple.

The miracle came. We obtain a search help directly by clicking on its icon based on the value of the field above.


Code snippets:

Screen:


  SELECTION-SCREEN BEGIN OF BLOCK frame1.
    " Name of SQL table
    SELECTION-SCREEN BEGIN OF LINE.
      SELECTION-SCREEN COMMENT 1(25TEXT-001.
      SELECTION-SCREEN POSITION 26"POS_LOW
      PARAMETERSp_tab LIKE dd02l-tabname.
    SELECTION-SCREEN END OF LINE.
    " Sort by table column
    SELECTION-SCREEN BEGIN OF LINE.
      SELECTION-SCREEN COMMENT 1(25TEXT-005.
      SELECTION-SCREEN POSITION 26.
      PARAMETERSp_sort TYPE slis_fieldname.
    SELECTION-SCREEN END OF LINE.
    " Filter one column
    SELECTION-SCREEN BEGIN OF LINE.
      SELECTION-SCREEN COMMENT 1(25TEXT-006.
      SELECTION-SCREEN POSITION 26.
      PARAMETERSp_filtr TYPE slis_fieldname.
      SELECTION-SCREEN COMMENT 60(10TEXT-007.
      SELECTION-SCREEN POSITION 71.
      PARAMETERSp_c_val TYPE string.
    SELECTION-SCREEN END OF LINE.
  SELECTION-SCREEN END OF BLOCK frame1.

Events:

Its a simple call of class method for both inputs (sorting + filtering) in this case.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_sort.
  gr_table->f4_for_colnameiv_scr_field_name 'P_SORT' ).

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_filtr.
  gr_table->f4_for_colnameiv_scr_field_name 'P_FILTR' ).



Search help code:


Method declaration

  PRIVATE SECTION.

    TYPESBEGIN OF sty_col,
      fieldname TYPE dd03l-fieldname,
    END OF sty_col.
    TYPEStty_cols TYPE STANDARD TABLE OF sty_col.
    METHODS:
      f4_for_colname
        IMPORTING iv_scr_field_name TYPE help_info-dynprofld.

Method implementation


  METHOD f4_for_colname.

    DATAit_cols      TYPE STANDARD TABLE OF sty_col,
          it_scr_field TYPE STANDARD TABLE OF rsselread,
          ls_scr_field LIKE LINE OF it_scr_field.

    MOVE 'P_TAB' TO ls_scr_field-name.
    MOVE 'P' TO ls_scr_field-kind" P for parameter, S for select option
    APPEND ls_scr_field TO it_scr_field.

    CALL FUNCTION 'RS_SELECTIONSCREEN_READ'
      EXPORTING
        program     sy-repid
        dynnr       sy-dynnr
      TABLES
        fieldvalues it_scr_field.

    READ TABLE it_scr_field INTO ls_scr_field INDEX 1.

    IF ls_scr_field-fieldvalue IS NOT INITIAL.
      TRANSLATE ls_scr_field-fieldvalue TO UPPER CASE" table name
      SELECT fieldname
        FROM dd03l
        INTO CORRESPONDING FIELDS OF TABLE it_cols
        WHERE tabname ls_scr_field-fieldvalue.
    ENDIF.

    CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
      EXPORTING
        retfield    'fieldname' " from itab
        dynpprog    sy-repid
        dynpnr      sy-dynnr
        dynprofield iv_scr_field_name " e.g. 'P_SORT'
        value_org   'S' " structure
      TABLES
        value_tab   it_cols.

  ENDMETHOD.                    "f4_for_colname


Sources: