1

I am using cl_abap_structdescr->get_components to get a list of fields in a structure. It works fine when I use it on locally declared structure types, but when I use it on DDIC structures, it doesn't give me the results I expect.

Reproducable example:

TYPES: BEGIN OF gty_outtab,
  infty TYPE infty,
  uname TYPE uname,
  bdate TYPE datum,
  btime TYPE uzeit,
  pernr TYPE pernr_d,
  opera TYPE hr_opera,
  begda TYPE begda,
  endda TYPE endda,
END OF gty_outtab.

DATA: lr_infty_structdescr  TYPE REF TO cl_abap_structdescr,
      lr_outtab_structdescr TYPE REF TO cl_abap_structdescr,
      lt_outtab_components  TYPE STANDARD TABLE OF abap_componentdescr,
      lt_infty_components   TYPE STANDARD TABLE OF abap_componentdescr.

" works as expected
lr_outtab_structdescr ?= cl_abap_structdescr=>describe_by_name( 'GTY_OUTTAB' ).
lt_outtab_components = lr_outtab_structdescr->get_components( ).

" doesn't work as expected
lr_infty_structdescr ?= cl_abap_structdescr=>describe_by_name( 'P0008' ).
lt_infty_components = lr_infty_structdescr->get_components( ).

BREAK-POINT.

Results:

That's okay for GTY_OUTTAB:

enter image description here

There are only two fields for P0008 although it contains many more fields (see below):

enter image description here

I already tried using cl_abap_typedescr instead and googled, but every code I find online looks just like mine?

Here is the definition of P0008 which contains many fields as you can see:

DDIC structure P0008 in SE11 Hierarchy display

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Legxis
  • 886
  • 7
  • 19

4 Answers4

4

Of course after posting this, I found the reason why (german thread). Apparently, if the given structure contains included structures, then get_components won't break them up. Three solutions have been suggested in the thread and they all work great for me. Since I only need the structures' fieldnames, I will use option 1.

DATA: lt_infty_complist1 TYPE abap_compdescr_tab,
      lt_infty_complist2 TYPE STANDARD TABLE OF fieldname,
      lt_infty_complist3 TYPE abap_component_tab.

" 1 - get full fieldname list, but with barely any typedescription
lt_infty_complist1 = lr_infty_structdescr->components.

" 2 - get full fieldname list of DDIC structures, but without typedescription
SELECT fieldname
  FROM dd03l
  INTO TABLE lt_infty_complist2
 WHERE tabname = 'P0008'.

DELETE lt_infty_complist2 WHERE table_line = '.INCLU--AP'
                             OR table_line = '.INCLUDE'.

" 3 - get full component list 
" function code from: https://www.abapforum.com/forum/viewtopic.php?f=18&p=59840)
PERFORM return_components USING lr_infty_structdescr CHANGING lt_infty_complist3.
Legxis
  • 886
  • 7
  • 19
1

Method GET_RTTS_FOR_LOCAL_TABLE of helper-class CL_CACS_RTTS_HELPER seems to do exactly what you want and what lacks your option 1

CALL METHOD cl_cacs_rtts_helper=>get_rtts_for_local_structure
  EXPORTING
    id_tabname = 'P0008'
  receiving
    ro_data    = DATA(ro_struct)
.

It fetches all components of a structure into reference data object and also includes absolute types:

enter image description here

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • 1
    Thank you, that seems very convenient. I'll mark this as the answer, I can't do it like that though, because this class doesn't exist on my release (ABAP 700, no enhancement packages). – Legxis Oct 18 '19 at 08:24
  • Yep, doesn't exist on my bare 700 SP05 either, but exists on 740 EHP7 SP04. Seems it had been introduced somewhere between 7.31 and 7.40. – Suncatcher Oct 18 '19 at 18:44
1

In the same class there is get field list method, which may be sufficient.

data: lo_incl_stru TYPE REF TO cl_abap_structdescr,
       lt_field_list TYPE ddfields.

    lt_field_list =  lo_incl_stru->get_ddic_field_list( p_including_substructres = abap_true ).

If that isnt enough.... try

 METHODS recursive_get_components
      IMPORTING
        io_structdescr       TYPE REF TO cl_abap_structdescr
      RETURNING
        VALUE(rt_components) TYPE abap_component_tab.


METHOD recursive_get_components.
    DATA:
      lo_incl_stru TYPE REF TO cl_abap_structdescr,
      lt_incl_comp TYPE abap_component_tab,
      l_curr_index TYPE i.

    FIELD-SYMBOLS: <comp>      LIKE LINE OF rt_components,
                   <incl_comp> LIKE LINE OF lt_incl_comp.


    rt_components = io_structdescr->get_components( ).

    LOOP AT rt_components ASSIGNING <comp>.
      IF <comp>-as_include = 'X'.
        lo_incl_stru ?= <comp>-type.  " not the include struc type
        l_curr_index = sy-tabix.      " and the index it is to be included
        DELETE rt_components INDEX l_curr_index.

        lt_incl_comp = recursive_get_components( io_structdescr =  lo_incl_stru  ).
        LOOP AT lt_incl_comp ASSIGNING <incl_comp>.
          INSERT <incl_comp> INTO rt_components INDEX l_curr_index.
          l_curr_index = l_curr_index + 1.
        ENDLOOP.
      ENDIF.

    ENDLOOP.


  ENDMETHOD.
phil soady
  • 11,043
  • 5
  • 50
  • 95
1

There is a method on cl_abap_structdescr called get_included_view( ) that will expand the included structures

Ahsoka91
  • 36
  • 2
  • Thank you, this is exactly what I wanted, and it was right before my eyes the whole time, haha – Legxis Oct 21 '19 at 08:47