Skip to Content
avatar image
Former Member

Get name of structure field

Hi Gurus!

I need get a string with field name. For example:

TYPES: BEGIN OF t_mara,

               material TYPE matnr,

               plant      TYPE werks,

             END OF t_mara.

I can get field names of structures using:

DATA: lr_rtti_struc TYPE REF TO cl_abap_structdescr.

DATA: lt_comp       TYPE cl_abap_structdescr=>component_table.

DATA: ls_comp       LIKE LINE OF lt_comp.

DATA: ls_mara       TYPE t_mara.

lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( ls_mara ). " Get the description of the data

lt_comp = lr_rtti_struc->get_components( ). "Get the fields of the structure

loop at lt_comp into ls_comp.

WRITE:/ ls_comp-name. " Write the column names

endloop.

With this code the result will be:

MATERIAL

PLANT

But i need get only description for a field as:

  DATA: lv_element TYPE REF TO cl_abap_datadescr.

  TRY.

      lv_element ?= cl_abap_datadescr=>describe_by_data( lw_mara-material ).

    CATCH cx_sy_move_cast_error.

  ENDTRY.

I get only description for the type of field, in this case "matnr". All class cl_abap_* only return data about types...


Thanks and regards,

Jose

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • May 28, 2015 at 03:33 PM

    Are you looking to find the Description of the field or the Type of the field? If you need Type of the field, use Hans Ceuppensmethod.

    If you need Description,

    1. Use function module DDIF_FIELDINFO_GET, passing the type, to get the details of type.

    2. Use methods CL_SALV_TABLE=>FACTORY & CL_SALV_CONTROLLER_METADATA=>GET_SLIS_FIELDCATALOG, to generate the Fieldcatalog and then read the Fieldcatalog to get the description.

    Thanks,

    Juwin Thomas

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Juwin,

      I'm try to find description of the field. Your code is to generate an alv, and i can't use function:

      CALL FUNCTION 'DDIF_FIELDINFO_GET'

              EXPORTING

                tabname              = ''

      *         FIELDNAME       = ''

      *         LANGU               = SY-LANGU

      '


      I can't use literals, for this reason i need to get description of field using lw_mara-matnr for example.


      Regards.

  • May 28, 2015 at 01:02 PM

    Hi José! ¡Hola José!

    A time ago I created a class method "GET_STRUCT_FIELDS", where it's possible to get informations about a structure passed by reference.

    Here you are:

    Method "GET_STRUCT_FIELDS":

    Parameters:

    I_STRUCTREF Importing Type ANY
    I_FLG_VALUE Importing Type FLAG
    E_OUTPUT Exporting Type ZSD_TT_GET_TABLE_FIELDS

    ZSD_TT_GET_TABLE_FIELDS => Table type of structure ZSD_ST_GET_TABLE_FIELDS, that have the follow components:

    NAME TYPE CHAR 30
    TYPE_KIND TYPE CHAR 1
    LENGTH TYPE NUMC 4
    DECIMALS TYPE NUMC 4
    VALUE TYPE STRING


    METHOD get_struct_fields.                                "#EC CI_VALPAR

      DATA:
        lw_tab                   TYPE REF TO data                          , "#EC NEEDED
        lv_nocomps               TYPE i                                    , "#EC NEEDED
        lv_ddtype                TYPE abap_bool                            , "#EC NEEDED
        lv_charsize              TYPE i                                    , "#EC NEEDED
        lt_output                TYPE zsd_tt_get_table_fields              , "#EC NEEDED
        lw_output                LIKE LINE OF lt_output                    . "#EC NEEDED

      FIELD-SYMBOLS:
        <fs_structcomp>          TYPE abap_compdescr                       , "#EC NEEDED
        <fs_fld_val>             TYPE any                                  . "#EC NEEDED

      lv_charsize = cl_abap_char_utilities=>charsize.

      GET REFERENCE OF i_structref INTO lw_tab.


      CALL METHOD cl_abap_tabledescr=>describe_by_data_ref
        EXPORTING
          p_data_ref  = lw_tab
        RECEIVING
          p_descr_ref = ccl_abap_typedescr.

      IF ccl_abap_typedescr->kind NE cl_abap_datadescr=>kind_struct.
        RETURN.
      ENDIF.

      ccl_abap_structdescr ?= ccl_abap_typedescr.


      lv_nocomps = lines( ccl_abap_structdescr->components ).

      IF lv_nocomps IS INITIAL.
        RETURN.
      ENDIF.


      LOOP AT ccl_abap_structdescr->components ASSIGNING <fs_structcomp>.

        CLEAR: lw_output.

        MOVE-CORRESPONDING <fs_structcomp> TO lw_output.

        IF lw_output-type_kind NE cl_abap_datadescr=>typekind_int        AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_int1       AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_int2       AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_intf       AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_iref       AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_decfloat   AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_decfloat16 AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_decfloat34 AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_float      AND
           lw_output-type_kind NE cl_abap_datadescr=>typekind_packed .

          lw_output-length = ceil( lw_output-length / lv_charsize ).

        ENDIF.


        IF NOT i_flg_value IS INITIAL.

          ASSIGN COMPONENT <fs_structcomp>-name OF STRUCTURE i_structref TO <fs_fld_val>.
          IF <fs_fld_val> IS ASSIGNED.

            MOVE <fs_fld_val> TO lw_output-value.
            UNASSIGN: <fs_fld_val>.

          ENDIF.

        ENDIF.

        APPEND lw_output TO lt_output.

      ENDLOOP.

      IF <fs_structcomp> IS ASSIGNED.
        UNASSIGN: <fs_structcomp>.
      ENDIF.

      IF NOT lt_output IS INITIAL.
        APPEND LINES OF lt_output TO e_output.
      ENDIF.

    ENDMETHOD.                                               "#EC CI_VALPAR

    I hope this be helpful for you!

    Good luck!

    PS: Also I created a similar method to get all information about a table passed by reference, so let me know if you will need it!

    Best regards from Brazil!

    Alexandre B. Dambrowski

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 28, 2015 at 01:28 PM

    Dear José,

    Try this code:

    DATA: lv_string TYPE string,

                lo_descr TYPE REF TO cl_abap_datadescr.

    lo_element ?= cl_abap_datadescr=>describe_by_name( 'THOCT-SPRAS' ).

    lv_string = lo_element->absolute_name.

    lv_string will contain '\TYPE=SPRAS'. You have to remove the '\TYPE' from the string.

    regards,

    Hans

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 28, 2015 at 01:59 PM

    Hi Hans and Alexandre,

    thanks for your replies, but the code from Alexandre is the same that my example and the code from Hans return the name of type, no the name of field.

    I don't wanna use literals for this reason i'm using work area.

    DATA:

    DATA: lv_string TYPE string,

               lo_descr TYPE REF TO cl_abap_datadescr,

               lo_element TYPE REF TO cl_abap_typedescr.

               lw_thoct type thoct.

    lo_element ?= cl_abap_datadescr=>describe_by_name(  lw_thoct-ltext ).

    lv_string = lo_element->absolute_name.

    In this case, lv_string will contain name of type 'FKTEXT' but i need return 'LTEXT'.

    Thanks in advance.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi José,

      try to post the whole problema you need to solve.

      I can't get why you need to get the column from your workarea. If you set your work area you already have the field, doens't make sense to me.

      You won't be able to call this dynamically unless you use field-symbol. How are you calling this code?

      Regards,

      Frisoni

  • May 30, 2015 at 02:05 AM

    Hi,

    1) If you define your structure based on a data dictionary structure, you can use function module ICL_DDIC_FIELDDESCRIP_GET to get the description field names.


    CALL FUNCTION 'ICL_DDIC_FIELDDESCRIP_GET'

        EXPORTING

          iv_tabname            =

          iv_fieldname          =

    *    IV_LANGU              =

    *    IV_TXTTYPE            = ' '

    *  IMPORTING

    *    EV_FIELDDESCRIP       =

    *    EV_FIELDTEXT          =

    *  EXCEPTIONS

    *    NOT_FOUND             = 1

    *    OTHERS                = 2

                .

      IF sy-subrc <> 0.

    * Implement suitable error handling here

      ENDIF.




    Add comment
    10|10000 characters needed characters exceeded

  • May 30, 2015 at 03:21 AM

    TYPES: BEGIN OF t_mara,

                    material TYPE matnr,

                    plant      TYPE werks,

                  END OF t_mara.

    DATA: lr_rtti_struc TYPE REF TO cl_abap_structdescr.

    DATA: lt_comp       TYPE cl_abap_structdescr=>component_table.

    DATA: ls_comp       LIKE LINE OF lt_comp.

    DATA: ls_mara       TYPE t_mara.

    DATA: wa_dd04t TYPE dd04t.

    DATA    lo_element_descr   TYPE REF TO cl_abap_elemdescr.

    lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( ls_mara ). " Get the description of the data

    lt_comp = lr_rtti_struc->get_components( ). "Get the fields of the structure

    LOOP AT lt_comp INTO ls_comp.

       WRITE:/ ls_comp-name. " Write the column name

      ENDLOOP.

      write:/ 'Field description'.

       TRY.

           lo_element_descr  ?= cl_abap_datadescr=>describe_by_data( ls_mara-material ).

         CATCH cx_sy_move_cast_error.

       ENDTRY.

       SELECT SINGLE * FROM  dd04t

        INTO wa_dd04t

        WHERE rollname = lo_element_descr->absolute_name+6 AND

              ddlanguage = sy-langu AND

              as4local   = 'A'.

       IF sy-subrc = 0.

         WRITE:/ wa_dd04t-ddtext.

       ENDIF.





    123333.JPG (12.3 kB)
    Add comment
    10|10000 characters needed characters exceeded