Get Domain Fixed Value Description – Better Way

To get domain fixed value description, in past, I have used function modules DD_DOMVALUES_GET, DD_DOMA_GET and select from table DD07L. All these methods requires you to specify domain name or some would say require you to hardcode domain name. Recently, I’ve found this elegant way, which doesn’t require to specify domain name. As long as you have variable defined with reference to domain or data element or table field which would eventually be linked to domain it works.

This method is based on Runtime Type Services (RTTS) classes. RTTS can find out domain just from variable and will return domain fix values, so you no longer have to specify domain name.

I will be using domain KOART for demo.

Get Domain Fixed Value Description

I use this domain to work out FI secondary index tables. For example, in domain D is for customer therefore secondary index tables for customer are BSID and BSAD. Similarly for vendor and GL accounts tables are BSIK, BSAK, BSIS and BSAD respectively.

Coming back to topic. Here is the code in form of local class and method which does the magic. From calling program you need to just pass the variable to method GET_DOMAIN_DESCR and it will return you the description of fixed values which is in variable itself.

*----------------------------------------------------------------------*
*       CLASS cl_domain_desc DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_domain_desc  DEFINITION.
  PUBLIC SECTION.
    METHODS : get_domain_descr
                IMPORTING
                    i_field  TYPE any
                    i_langu TYPE  syst-langu DEFAULT sy-langu
                RETURNING
                    value(return) TYPE ddtext .
ENDCLASS.                    "cl_domain_desc DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_domain_desc IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_domain_desc IMPLEMENTATION .
  METHOD get_domain_descr.

    DATA : lo_element TYPE REF TO cl_abap_elemdescr ,
           li_values  TYPE ddfixvalues       ,
           ls_value   TYPE ddfixvalue        .

    lo_element ?= cl_abap_typedescr=>describe_by_data( i_field ) .
    li_values =  lo_element->get_ddic_fixed_values( i_langu ) .
    READ TABLE li_values INTO ls_value WITH KEY low = i_field .
    IF sy-subrc = 0 .
      return = ls_value-ddtext .
    ENDIF.
  ENDMETHOD.                    "GET_DOMAIN_DESCR
ENDCLASS .                    "cl_domain_desc IMPLEMENTATION

This is sample program which demonstrate use of above method. I have four variables defined in different ways but they all point to domain KOART.

DATA : lo_domain_text_test TYPE REF TO cl_domain_desc .

DATA : lv_domain    TYPE koart      VALUE 'A' , "variable defined using domain
       lv_data_elem TYPE bkoart     VALUE 'K' , "variable defined using data element
       lv_tab_field TYPE bseg-koart VALUE 'D' , "variable defined using table field
       lv_like      LIKE lv_domain  VALUE 'M' . "variable defined using LIKE

DATA : lv_description  TYPE ddtext .

*--------------------------------------------------------------------*
* Start of Selection
*--------------------------------------------------------------------*

START-OF-SELECTION .

  CREATE OBJECT lo_domain_text_test .

  CLEAR lv_description .
  lv_description = lo_domain_text_test->get_domain_descr( lv_domain ) .
  WRITE : / 'Domain variable : ' , lv_description .


  CLEAR lv_description .
  lv_description = lo_domain_text_test->get_domain_descr( lv_data_elem ) .
  WRITE : / 'Data element variable : ' , lv_description .


  CLEAR lv_description .
  lv_description = lo_domain_text_test->get_domain_descr( lv_tab_field ) .
  WRITE : / 'Table field variable : ' , lv_description .

  CLEAR lv_description .
  lv_description = lo_domain_text_test->get_domain_descr( lv_like ) .
  WRITE : / 'Variable using LIKE : ' , lv_description .

From the output you can see that it works for all cases.

Get Domain Fixed Value Description

4 Comments

 Add your comment
  1. It’s perfect!!!

    I was trying to reach the domain in first place through the describe_by_data to then get the values. And of course, I wasn’t able to do that. In fact this is not necessary: the method get_ddic_fixed_values leads you right there in one single step.

    Thanks a lot,
    César Scheck

  2. Hello,

    First, I would thank you for the code sample, it’s clear and helpful.

    But KOART is also a data element and in this case, it appears the domain values are retrieved from the domain, but it is not.
    Should you find a way to get it working for domain names, I am interested :-)

    Best regards,
    Zhou Tsui

  3. Hello,

    Nice code.

    What about descriptions from a domain value table?

    Regards

Leave a Reply

1 Trackback

  1. It returns in domain values | Useful codes and notes for SAP (Pingback)