Dynamic Search Help from Internal Table Data

Aug 11, 2014

While SAP has created most of the Search Helps that we will ever need, we still face situations where a Search Help has to be shown dynamically from data that we have in the an internal table. SAP has provided us a powerful Function Module to acheive this, and that is F4IF_INT_TABLE_VALUE_REQUEST.

This Function Module gets an internal table, list of fields that should be shown in the Search Help and mapping of other screen fields to the internal table fields (to fill those fields automatically based on selection) as input, though the last two are optional.

Now we are going to develop a program which has two fields in the Selection Screen which are Sales Document Number and Sold To Party. On pressing F4 in Sales Document Number we will be displaying the Search Help with three fields(Sales Document Number, Created Date, Sold To Party) and on selecting a particular record, we will fill the two fields in the Selection Screen correspondingly.

Search Help Using Itab

And on selecting a particular record, it fill both the fields.

Search Help Using Itab

The program for this example follows,

report  z_f4_internal_table.

parameters:
  p_vbeln type c length 20,
  p_kunnr type c length 10.

at selection-screen on value-request for p_vbeln.

  data:
        lt_vbak type standard table of vbak,
        lt_fields type standard table of dfies,
        lt_mapping type standard table of dselc.

  "Fetching only 100 sales documents
  select * from vbak
    into table lt_vbak
    up to 100 rows.

  "Adding the fields we need to be shown in the
  "search help
  perform add_field
    tables lt_fields
    using 'VBAK'
          'VBELN'.

  perform add_field
    tables lt_fields
    using 'VBAK'
          'ERDAT'.

  perform add_field
    tables lt_fields
    using 'VBAK'
          'KUNNR'.

  "Adding fields that has to be mapped to
  "selection screen fields
  perform append_mapping
    tables lt_mapping
    using 'KUNNR' 'P_KUNNR'.

  call function 'F4IF_INT_TABLE_VALUE_REQUEST'
    exporting
     retfield               = 'VBELN'
     dynpprog               = sy-repid
     dynpnr                 = sy-dynnr
     dynprofield            = 'P_VBELN'
     value_org              = 'S'
    tables
     value_tab              = lt_vbak
     field_tab              = lt_fields
     dynpfld_mapping        = lt_mapping
    exceptions
     parameter_error        = 1
     no_values_found        = 2
     others                 = 3
            .
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

*&------------------------------------------------------*
*&      Form  add_field
*&------------------------------------------------------*
*  Add field info to fieldtab given its table name and
*  field name
*-------------------------------------------------------*
*      -->TABNAME    text
*      -->FIELDNAME  text
*-------------------------------------------------------*
form add_field
  tables p_fields
  using tabname type tabname
        fieldname type fieldname.


  data: ls_field type dfies,
       lt_fields type standard table of dfies.

  call function 'DDIF_FIELDINFO_GET'
   exporting
     tabname              = tabname
     fieldname            = fieldname
   tables
     dfies_tab            = lt_fields
   exceptions
     not_found            = 1
     internal_error       = 2
     others               = 3
            .
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

  append lines of lt_fields to p_fields.

endform.                    "add_field

*&------------------------------------------------------*
*&      Form  append_mapping
*&------------------------------------------------------*
*  Append field mapping to mapping table
*-------------------------------------------------------*
*      -->P_MAPPING       Mapping table
*      -->P_FIELD         Field in Itab
*      -->P_DYNPRO_FIELD  Field in screen
*-------------------------------------------------------*
form append_mapping
  tables p_mapping
  using p_field
        p_dynpro_field.

  data:
        ls_mapping type dselc.

  ls_mapping-fldname = p_field.
  ls_mapping-dyfldname = p_dynpro_field.
  append ls_mapping to p_mapping.

endform.                    "append_mapping

As the program is very much self-explanatory, I would like to point out only the inputs given to F4IF_INT_TABLE_VALUE_REQUEST and its purpose.

Though the Function Module contains more fields, I have chosen minimal fields that will be used for most cases just to avoid confusion.