fareez.info

Dynamic Search Help from Internal Table Data

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.

  • retfield is the field name in the internal table which should be returned as the value to Selection Screen field

  • dynprog and dynpnr takes the report id and screen number respectively

  • dynprofield is the Selection Screen field name for which the search help is bound with and result of the selection will be filled in this field

  • value_org is the field which defines how the data should be displayed from the internal table in the Search Help. It takes 'C' and 'S' as input. 'S' is to display the data according to the structure of the internal table and 'C' is for cell by cell rendering which is least useful. For showing multiple columns of data in a Search Help you will mostly need 'S'

  • value_tab is the internal table which contains data for selection

  • field_tab is the list of fields you would like the Search Help to display. It is optional. When it is not given, all fields will be displayed (value_org must be 'S'). For Data Dictionary objects, you can fetch the field details using the function module DDIF_FIELDINFO_GET

  • dynpfld_mapping gets the dynpro field to internal table field mapping, for filling multiple fields based on the selection in the Search Help. It is also optional

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

comments powered by Disqus