When working on SAP ABAP applications, there are many scenarios where we need to display messages dynamically. A common requirement is:
“Show a message like
Material is mandatory
without hardcoding the field name.”
If you hardcode labels, you’re creating a maintenance headache, especially when the field descriptions change in the Data Dictionary. Wouldn’t it be better if ABAP could fetch the label from DDIC directly at runtime?
That’s exactly what the below logic does.
The Code Logic
Here’s a ready-to-use snippet for fetching the label of any ABAP field:
DATA: lv_field TYPE werks_d, " Sample field for demo
lv_label TYPE string. " Field label result
TRY.
" Get RTTI type description for the passed value
DATA(lo_descr) = cl_abap_typedescr=>describe_by_data( lv_field ).
IF lo_descr IS BOUND AND lo_descr->kind = cl_abap_typedescr=>kind_elem.
DATA(lo_ele_descr) = CAST cl_abap_elemdescr( lo_descr ).
" Get associated data element from DDIC
DATA(lv_data_element) = lo_ele_descr->get_ddic_field( ).
IF lv_data_element IS NOT INITIAL.
" Fetch field label from DDIC text table
SELECT SINGLE scrtext_l
INTO lv_label
FROM dd04t
WHERE rollname = lv_data_element
AND ddlanguage = sy-langu.
ENDIF.
ENDIF.
WRITE: lv_label.
CATCH cx_sy_open_sql_db INTO DATA(lx_sql).
CATCH cx_root INTO DATA(lx_root).
ENDTRY.
Step-by-Step Explanation
-
RTTI Type Description
cl_abap_typedescr=>describe_by_data( lv_field )
This returns an object describing the data type of the given variable using RTTI (Run Time Type Information). -
Check if It’s an Elementary Type
We ensure the field is an elementary type (not a structure or table) before proceeding. -
Cast to Element Descriptor
We castlo_descr
tocl_abap_elemdescr
, which gives us access to methods specific to elementary data types. -
Get Data Element Name
get_ddic_field()
returns the Data Element associated with the field from the Data Dictionary. -
Fetch the Field Label
We query DD04T, the SAP text table for Data Elements, to get the long text (scrtext_l
) for the user’s logon language. -
Output the Result
The result (lv_label
) now contains the field’s descriptive label.
How to Use This Code
You can wrap the above logic in a reusable method or function module. Example method signature:
METHOD get_field_label.
IMPORTING iv_field TYPE any
RETURNING VALUE(rv_label) TYPE string.
Then use the method wherever needed.
This ensures that your messages are dynamic, language-dependent, and always aligned with the latest DDIC definitions.
Practical Use Cases
-
Dynamic Validation Messages
Display “Material Number is mandatory
” instead of “MAT001 is mandatory
” without hardcoding. -
Generic Message Classes
Build reusable messages where&
is replaced by actual field labels at runtime. -
UI Enhancements
Show labels dynamically in custom ALV, Fiori, or Web Dynpro applications without maintaining separate text mappings. -
Logging and Error Reports
Store meaningful field names in logs for better readability.
Why This Approach Helps
-
Language-Dependent: Labels are fetched in the user’s logon language automatically.
-
Maintenance-Free: No need to update your code if a label changes in the Data Dictionary.
-
Generic & Reusable: Works with any field, just pass the variable and get the label.