Below is an example of an ABAP method that reads domain texts (descriptions) for a given domain. Domain texts are stored in the table DD07T, which contains the texts for domains and their fixed values.
Method to Read Domain Texts
This method will:
Accept a domain name as input.
Read the domain texts from the DD07T table.
Return the texts in an internal table for further processing.
abap
CLASS zcl_domain_text_reader DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
TYPES: BEGIN OF ty_domain_text,
domvalue_l TYPE dd07t-domvalue_l, " Domain value (low)
ddtext TYPE dd07t-ddtext, " Domain value text
END OF ty_domain_text.
TYPES: tt_domain_texts TYPE TABLE OF ty_domain_text WITH DEFAULT KEY.
METHODS read_domain_texts
IMPORTING
iv_domain_name TYPE dd07t-domname
RETURNING
VALUE(rt_texts) TYPE tt_domain_texts
RAISING
cx_sy_sql_error.
ENDCLASS.
CLASS zcl_domain_text_reader IMPLEMENTATION.
METHOD read_domain_texts.
" Clear the return table
CLEAR rt_texts.
" Select domain texts from DD07T
SELECT domvalue_l ddtext
INTO TABLE rt_texts
FROM dd07t
WHERE domname = iv_domain_name
AND ddlanguage = sy-langu. " Use current system language
IF sy-subrc <> 0.
" If no texts are found, raise an exception or handle accordingly
MESSAGE e000(oo) WITH 'No texts found for domain' iv_domain_name INTO DATA(lv_msg).
RAISE EXCEPTION TYPE cx_sy_sql_error
EXPORTING
textid = cx_sy_sql_error=>sql_error
msg = lv_msg.
ENDIF.
ENDMETHOD.
ENDCLASS.
Explanation of the Code
Class Definition:
The class zcl_domain_text_reader is defined with a public method read_domain_texts.
A custom type ty_domain_text is defined to hold the domain value (domvalue_l) and its corresponding text (ddtext).
A table type tt_domain_texts is defined to store multiple domain texts.
Method read_domain_texts:
Input Parameter: iv_domain_name (the name of the domain, e.g., 'ZDOMAIN').
Output Parameter: rt_texts (an internal table containing the domain values and their texts).
The method queries the DD07T table, which stores domain fixed values and their texts.
The ddlanguage is set to sy-langu to fetch texts in the current system language.
If no texts are found, an exception is raised.
Exception Handling:
The method raises a cx_sy_sql_error exception if no texts are found or if there is an SQL error.
Example Usage
Here’s how you can use the above method in a program:
abap
REPORT z_test_domain_texts.
DATA: lo_reader TYPE REF TO zcl_domain_text_reader,
lt_texts TYPE zcl_domain_text_reader=>tt_domain_texts.
START-OF-SELECTION.
TRY.
" Create instance of the class
CREATE OBJECT lo_reader.
" Call the method to read domain texts
lt_texts = lo_reader->read_domain_texts( iv_domain_name = 'ZDOMAIN' ).
" Display the results
LOOP AT lt_texts INTO DATA(ls_text).
WRITE: / 'Domain Value:', ls_text-domvalue_l,
'Text:', ls_text-ddtext.
ENDLOOP.
CATCH cx_sy_sql_error INTO DATA(lx_error).
" Handle the exception
WRITE: / 'Error:', lx_error->get_text( ).
ENDTRY.
Notes
Domain Name: Replace 'ZDOMAIN' with the actual domain name you want to read texts for.
Language: The method uses sy-langu to fetch texts in the current system language. If you need texts in a specific language, modify the WHERE clause in the SELECT statement to use a different language code.
Fixed Values: This method is particularly useful for domains with fixed values (e.g., domains used in dropdowns or value ranges). If the domain does not have fixed values, the DD07T table will not contain any entries.
Authorization: Ensure the user has the necessary authorizations to read the DD07T table.
This approach provides a reusable and clean way to read domain texts in ABAP.