Copyright 2021 - BV TallVision IT

Pressing the F4 button to get an overview of possible values is a real winner on any SAP screen: not only does is allow the end user to select from possible values - it also demonstrates what the field is for, which has this clarifying effect on how the system works. Supply in the F4 response for yourselves:

Linking F4 help to a field on the selection screen

In this example we first supply our own F4 processing for a parameter field, then for the low and highvalues of a select-option field. The selection screen for both of these parameters:

parameters: p_mypar type char10. 
select-options: s_matnr for mara-matnr. 

at selection-screen on value-request for p_mypar.
  perform f4_my_own_variabele using 'P_MYPAR'. 
  
* Note that for select options, there are 2 fields available on the
* screen, both of which will need to be linked to F4 functionality  
at selection-screen on value-request for s_matnr-low.
  perform f4_material_number using 'S_MATNR-LOW'. 
at selection-screen on value-request for s_matnr-high.
  perform f4_material_number using 'S_MATNR-HIGH'. 

F4 possible values - for parameter

When the data dictionary (or domain values) is no help in producing a possible values list, it can also be composed by yourself. Follow the example: 

form f4_my_own_variabele using p_name. 

  TYPES: BEGIN OF lty_columns,
* List the fields you want to show in the F4 popup
           first TYPE char10, 
           second TYPE char10, 
           description TYPE char30, 
         END OF lty_columns.
  DATA: lt_columns TYPE STANDARD TABLE OF lty_columns.
  
  clear: lt_columns[]. 
* Set selection values / note this is just for example purpose:
  APPEND 'Cat.A     123       Category A - good stuff' to lt_columns.
  APPEND 'Cat.B     12523     Category A - great stuff' to lt_columns.
  APPEND 'Cat.X     6236      Category X - horrid stuff' to lt_columns.
  
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield               = 'SECOND' "<= target column field 
                                        "(on your internal table)
      dynpprog               = SY-REPID
      dynpnr                 = '1000' "<= parameter screen number 
	                                  "(default selection screen)
      dynprofield            = p_name "<= Parameter screen field
                                      "used for wildcard selections	  
*     WINDOW_TITLE           =
      value_org              = 'S' "<= indicates the selection 
	                               "screen field need to be checked
    TABLES
      value_tab              = lt_columns.
endform.

When your end user presses the F4 button, this would look something like this:

Note that getting the column title to look better than "F0002", just use table field declarations other than CHAR10. The system is clever enough to pick them up.

Linking F4 help to a select options field

Very similar to the above example, but this time we select possible values from the database (the much more commonly used way).

form f4_material_number using p_name. 

  TYPES: BEGIN OF lty_columns,
* List the fields you want to show in the F4 popup
           matnr TYPE makt-matnr, 
           maktx TYPE makt-maktx, 
         END OF lty_columns.
  DATA: lt_columns TYPE STANDARD TABLE OF lty_columns.
  
  clear: lt_columns[]. 
* Selection of actual data   
  select matnr maktx from makt into table lt_columns
    where spras = sy-langu. 
  
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield               = 'MATNR'  
      dynpprog               = SY-REPID
      dynpnr                 = '1000' 
      dynprofield            = p_name 
      value_org              = 'S'  
    TABLES
      value_tab              = lt_columns.
endform.

This is a great (and often underestimated) tool for your end user.

Fetch a value from the selection screen, before it was processed

In some (rare) cases, the end user already typed a value and then decides to press the F4 button. If you want to check this value and maybe use it to compose your possible values answer, simply read the (unprocessed) value from the screen with function module DYNP_VALUES_READ. The regular dynpro processing for selection screens has not been performed on the field yet, but you can still have access to it. An example:

PARAMETERS: pa_uname TYPE XUBNAME 
  MATCHCODE OBJECT SX_USER OBLIGATORY DEFAULT sy-uname.
PARAMETERS: pa_shlpn TYPE DD30T-SHLPNAME OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_shlpn.
* Pick the dynpro value directly from the screen
  data: lt_DYNPREAD type standard table of DYNPREAD,
        lw_DYNPREAD type DYNPREAD.

  lw_DYNPREAD-FIELDNAME = 'PA_UNAME'.
  clear lt_DYNPREAD[].
  append lw_DYNPREAD to lt_DYNPREAD.
  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      DYNAME                               = sy-repid
      DYNUMB                               = '1000'
      TRANSLATE_TO_UPPER                   = 'X'
    TABLES
      DYNPFIELDS                           = lt_DYNPREAD
    EXCEPTIONS
      OTHERS                               = 0.
  read table lt_DYNPREAD into lw_DYNPREAD index 1.
  pa_uname = lw_DYNPREAD-FIELDVALUE.

So the PA_UNAME field is updated with the value you can see on the screen, yet this value has not bee processed through the regular Process After Input cycle. Especially usefull for F4 processing, if the field content of your parameter is required.

There is also a DYNPRO_VALUES_WRITEand in case this is your area of interest, there is also a C-routine that could come in handy: this returns the main program, subprogram, screen and field information on the field that was F4-ed on.

  CALL 'DY_GET_FOCUS'
        ID 'SSCREENNAM' FIELD focus-subprog
        ID 'SSCREENNBR' FIELD focus-subnum
        ID 'MSCREENNAM' FIELD focus-mainprog
        ID 'MSCREENNBR' FIELD focus-mainnum
        ID 'FIELDNAME' FIELD focus-fieldname
        ID 'FIELDOFFS' FIELD focus-offs
        ID 'LINE' FIELD focus-line.

(Source: function module F4IF_INT_TABLE_VALUE_REQUEST).

Strange behaviour ?

Set the value_org field to S and check again. I've noticed strange behavior where no values were displayed or whether the first column looks fine, the second column contains a concatenated first and second column AND every second value is lost.