简体   繁体   中英

Getting the fieldnames from the field-symbols

I need to get the field names in the field-symbol <itab> , so that I can use the names for the field catalog of ALV.

So I used cl_abap_structdescr but it throws me always an error. I tried this with an internal table and i had the expected result, but I have to use the field symbol instead of internal table.

ASSIGN lo_itab->* TO <itab>

data: go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.


  go_struct ?= cl_abap_typedescr=>describe_by_data( <itab> ).
  gt_comp = go_struct->get_components( ).

  loop at gt_comp into gs_comp.

      PERFORM fill_fieldcat USING : 
     gs_comp-name      ''       gs_comp-name
   .
  endloop.

this is the error; 在此处输入图片说明

Because <itab> is obviously an internal table , its type is "table", not "structure"! (also see the short dump, it says that describe_by_data returned a type cl_abap_tabledescr which is not compatible with the type of target go_struct , ie cl_abap_structdescr )

So you must first get its table type, then get the type of its lines (I assume here that it's a structured type, but it could be other types in some other situations).

data: go_table type ref to cl_abap_tabledescr.
      go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.

go_table ?= cl_abap_typedescr=>describe_by_data( <itab> ).
go_struct ?= go_table->get_table_line_type( ).
gt_comp = go_struct->get_components( ).
...

As you are assigning reference type to another reference type therefore you getting dump. Define a structure type and pass in place of as i did in the below example. You will not get any dump.

data: go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.

  DATA ls_spfli TYPE spfli.
  go_struct ?= cl_abap_typedescr=>describe_by_data( ls_spfli ).
  gt_comp = go_struct->get_components( ).

  loop at gt_comp into gs_comp.

*      PERFORM fill_fieldcat USING :
*     gs_comp-name      ''       gs_comp-name
*   .
  endloop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM