12-11-2008 3:09 PM
Hi,
In my program, I have a structure created via begin-of/end-of.
A table is based on this structure and later on, I use a macro that deals with the field names and values.
I'd like to make the macro dynamic, in that it gets the table field names with hardcoded values.
Is there anyway to get the names of my structure field names via an FM or through coding?
Thanks,
John
12-11-2008 3:28 PM
12-11-2008 3:40 PM
For future reference, answer is in thread
data: gs_t001 type t001.
data: go_struct type ref to cl_abap_structdescr,
gt_comp type abap_component_tab,
gs_comp type abap_componentdescr.
start-of-selection.
go_struct ?= cl_abap_typedescr=>describe_by_data( gs_t001 ).
gt_comp = go_struct->get_components( ).
loop at gt_comp into gs_comp.
write: / gs_comp-name.
endloop.
12-11-2008 3:49 PM
Two ways:
1) If you define your structure based on a data dictionary structure, you can use function module DDIF_TABL_GET to get the structure field names.
2) In NetWeaver version 6.0 and above, you can use Run Time Type Services (RTTS) classes for this purpose.
Here is sample code for RTTS:
data: BEGIN OF wa,
field1 type c,
field2 type d,
field3 type i,
END OF wa.
DATA: r_descr TYPE REF TO cl_abap_structdescr,
wa_comp TYPE abap_compdescr.
r_descr ?= cl_abap_typedescr=>describe_by_data( wa ).
LOOP AT r_descr->components INTO wa_comp.
WRITE:/ wa_comp-name.
ENDLOOP.