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: 

How to get number of fields of a dynamic work area.

0 Kudos

Hi All,

I have a dynamic internal table. I want to get the number of fields in the work area. Is there any FM for the same?

Thanks,

Samriddhi

1 ACCEPTED SOLUTION

Hi,

You can refer to cl_abap_structdescr and cl_abap_typedescr

Here is a snippet

 data lr_structdescr type ref to cl_abap_structdescr,
      lt_components type abap_component_tab,
      l_component   like line of lt_components.

  lr_structdescr ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).  "here you describe your field symboL
  lt_components = lr_structdescr->get_components( ). "get fields names into internal table
"check out LT_COMPONENTS in debugger for your needs

Regards,

Nam

7 REPLIES 7

Hi,

You can refer to cl_abap_structdescr and cl_abap_typedescr

Here is a snippet

 data lr_structdescr type ref to cl_abap_structdescr,
      lt_components type abap_component_tab,
      l_component   like line of lt_components.

  lr_structdescr ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).  "here you describe your field symboL
  lt_components = lr_structdescr->get_components( ). "get fields names into internal table
"check out LT_COMPONENTS in debugger for your needs

Regards,

Nam

0 Kudos

Hi Nam,

I tried this and it worked. Thanks! 🙂

Regards,

Samriddhi

cenkay
Explorer
0 Kudos

Hi Samriddhi,

DATA(lines) = lines( CAST cl_abap_structdescr(
                         CAST cl_abap_tabledescr(
                                    cl_abap_tabledescr=>describe_by_name( p_name = 'MAKT_TAB' ) )->get_table_line_type( )
                                    "cl_abap_tabledescr=>describe_by_data( p_data = 'Your Internal Table' ) )->get_table_line_type( )
                            )->components ).

Regards,

Cenkay

Sandra_Rossi
Active Contributor
0 Kudos

describe_by_name is quite unusual, we use more often cl_abap_tabledescr=>describe_by_data( <dynitab> or paramitab ) (without quotes around!)

0 Kudos

Hi Cenky,

Thanks for the answer! However, This is throwing me a dump.

Regards,

Samriddhi

Sandra_Rossi
Active Contributor
0 Kudos

@nsamriddhi probably you didn't adapt the code adequately, no doubt it should work (and it's basically the same answer as Nam but with more complete code).

cenkay
Explorer
0 Kudos

Hi Samriddhi,

Did you assigned your dynamic tab to internal table?

Basic demo program:

TYPES : tty_mseg TYPE TABLE OF mseg.
DATA : gr_tab   TYPE REF TO data.
FIELD-SYMBOLS : <fs_tab> TYPE STANDARD TABLE.

"creation demo table
CREATE DATA gr_tab TYPE tty_mseg.
"assign data to itab
ASSIGN gr_tab->* TO <fs_tab>.


TRY.
    DATA(gv_lines) = lines( CAST cl_abap_structdescr(
                             CAST cl_abap_tabledescr(
                                        cl_abap_tabledescr=>describe_by_data( p_data = <fs_tab> ) )->get_table_line_type( )
                                )->components ).
  CATCH cx_sy_move_cast_error.
ENDTRY.


cl_demo_output=>display_text( text = |{ gv_lines }| ).