Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Get name of structure field

jmtrujillogalan
Explorer
0 Kudos

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

9 REPLIES 9

Former Member
0 Kudos

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

Former Member
0 Kudos

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

jmtrujillogalan
Explorer
0 Kudos

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.

0 Kudos

José,

If I reread your question, I think you want to have the name of the filed in a local variable.

Like:

Types: BEGIN OF t_mara,

               material TYPE matnr,

               plant      TYPE werks,

             END OF MATNR.

DATA: ls_mara TYPE t_mara.

Why not working with CONSTANTS: con_material TYPE string VALUE 'material'. because you have the name of the field in you program while you are programming ? Or do you work with dynamically assigned variables ?

Regards,

Hans

0 Kudos

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

Juwin
Active Contributor
0 Kudos

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

0 Kudos

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.

former_member184158
Active Contributor
0 Kudos

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.




former_member184158
Active Contributor
0 Kudos

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.