Dynamic validation of manual input
I faced a scenario where I had an ALV grid where all columns were of the same generic type (let’s make it simple) CHAR255. Each column has its name in field catalog in format TABNAME-FIELDNAME (+ more human readable column header texts of course).
What I needed to achieve was to make validation of data that user entered in the ALV cells. Since the cell validation is not available by default (because of type CHAR255) I had to make it dynamically.
In this article I’d like to share my solution
If you know the Data Dictionary table name and field name you can get the search help name (if it exists) by calling a FM F4IF_DETERMINE_SEARCHHELP.
If this module returns valid data you can use it to call second FM called F4IF_SELECT_VALUES which returns itab with values that are normally displayed when the search help is triggered.
Generally the second FM can return enormous number of results so it’s wise to limit the search with a filter (filter the one and only value – which was entered by the user).
If the second FM returns any result, it means the value is accepted as valid.
If no result is returned, it means the value enetered by user is not valid for the given field.
Now let’s take a look at how exactly this can be implemented:
DATA: * Table and field name you get during runtime g_tabname TYPE dfies-tabname, g_fieldname TYPE dfies-fieldname, * Search help helper variables gs_shlp TYPE shlp_descr, gt_allowed_values TYPE TABLE OF ddshretval. * Constants used for testing CONSTANTS: gc_test_werks_ok TYPE werks_d VALUE '2021', gc_test_werks_error TYPE werks_d VALUE '6058'. FIELD-SYMBOLS: <fs_selopt> TYPE ddshselopt. * We are testing against MARC table and its field WERKS g_tabname = 'MARC'. g_fieldname = 'WERKS'. * Get the search help if it exists/is defined CALL FUNCTION 'F4IF_DETERMINE_SEARCHHELP' EXPORTING tabname = g_tabname fieldname = g_fieldname IMPORTING shlp = gs_shlp EXCEPTIONS field_not_found = 1 no_help_for_field = 2 inconsistent_help = 3 OTHERS = 4. IF sy-subrc = 0. * Check if its a collective search help - in this case pick first one from list of included search helps CALL FUNCTION 'DD_SHLP_EXPAND_HELPMETHOD' EXPORTING shlp_top = gs_shlp IMPORTING shlp_tab = gt_shlp_tab. CLEAR gs_shlp. CHECK gt_shlp_tab IS NOT INITIAL. READ TABLE gt_shlp_tab INDEX 1 INTO gs_shlp. * Test with correct plant APPEND INITIAL LINE TO gs_shlp-selopt ASSIGNING <fs_selopt>. <fs_selopt>-shlpname = gs_shlp-shlpname. <fs_selopt>-shlpfield = g_fieldname. <fs_selopt>-sign = 'I'. <fs_selopt>-option = 'EQ'. <fs_selopt>-low = gc_test_werks_ok. CLEAR gt_allowed_values. * Collect values from search help filtered * by the plant user entered CALL FUNCTION 'F4IF_SELECT_VALUES' EXPORTING shlp = gs_shlp call_shlp_exit = 'X' TABLES return_tab = gt_allowed_values. IF gt_allowed_values IS INITIAL. WRITE:/ ' Plant ', gc_test_werks_ok, ' is not valid'. ELSE. WRITE:/ ' Plant ', gc_test_werks_ok, ' is OK'. ENDIF. * Test with invalid plant CLEAR gs_shlp-selopt. APPEND INITIAL LINE TO gs_shlp-selopt ASSIGNING <fs_selopt>. <fs_selopt>-shlpname = gs_shlp-shlpname. <fs_selopt>-shlpfield = g_fieldname. <fs_selopt>-sign = 'I'. <fs_selopt>-option = 'EQ'. <fs_selopt>-low = gc_test_werks_error. CLEAR gt_allowed_values. CALL FUNCTION 'F4IF_SELECT_VALUES' EXPORTING shlp = gs_shlp * call_shlp_exit = 'X' TABLES return_tab = gt_allowed_values. IF gt_allowed_values IS INITIAL. WRITE:/ ' Plant ', gc_test_werks_error, ' is not valid'. ELSE. WRITE:/ ' Plant ', gc_test_werks_error, ' is OK'. ENDIF. ENDIF.
The output (depending on data in your system) will be like on picture below:
The original post is on my blog at oprsteny.com