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 attributes from structure

Former Member
0 Kudos
1,453

Hi Experts,

it is possible get all attributes from a structure in a loop or so on?

e.g.

<structure>-<attribute> = Loop at <structure>.

Thanks in advance

1 ACCEPTED SOLUTION

Former Member
0 Kudos
309

Thanks, thats work if define my own structure.

But if I use the structure CRMT_BUPA_IL_HEADER_SEARCH I only get I subset of sttributes?

data: ls_struct type CRMT_BUPA_IL_HEADER_SEARCH.

DATA:

lr_structdescr TYPE REF TO cl_abap_structdescr,

lr_datadescr TYPE REF TO cl_abap_datadescr,

lt_components TYPE abap_component_tab,

ls_components TYPE LINE OF abap_component_tab.

  • now decribe the LS_STRUCT structure

lr_structdescr ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( ls_struct ).

lt_components = lr_structdescr->GET_COMPONENTS( ).

loop at lt_components into ls_components.

lr_datadescr = ls_components-type.

write:/ ls_components-name, lr_datadescr->TYPE_KIND, lr_datadescr->length.

endloop.

6 REPLIES 6

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
309

You can do something like this to describe the fields of a structure.

TYPE-POOLS: abap.

Types: begin of t_struct,
        var1 type i,
        var2 type string,
        var3 type c,
       end of t_Struct.

data: ls_struct type t_struct.

DATA:
  lr_structdescr    TYPE REF TO cl_abap_structdescr,
  lr_datadescr      TYPE REF TO cl_abap_datadescr,
  lt_components     TYPE abap_component_tab,
  ls_components     TYPE LINE OF abap_component_tab.

* now decribe the LS_STRUCT structure
  lr_structdescr ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( ls_struct ).
  lt_components = lr_structdescr->GET_COMPONENTS( ).
  loop at lt_components into ls_components.
    lr_datadescr = ls_components-type.
    write:/ ls_components-name, lr_datadescr->TYPE_KIND, lr_datadescr->length.
  endloop.

Regards,

Rich Heilman

Former Member
0 Kudos
310

Thanks, thats work if define my own structure.

But if I use the structure CRMT_BUPA_IL_HEADER_SEARCH I only get I subset of sttributes?

data: ls_struct type CRMT_BUPA_IL_HEADER_SEARCH.

DATA:

lr_structdescr TYPE REF TO cl_abap_structdescr,

lr_datadescr TYPE REF TO cl_abap_datadescr,

lt_components TYPE abap_component_tab,

ls_components TYPE LINE OF abap_component_tab.

  • now decribe the LS_STRUCT structure

lr_structdescr ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( ls_struct ).

lt_components = lr_structdescr->GET_COMPONENTS( ).

loop at lt_components into ls_components.

lr_datadescr = ls_components-type.

write:/ ls_components-name, lr_datadescr->TYPE_KIND, lr_datadescr->length.

endloop.

0 Kudos
309

I suppose you must be referring to fields that are parts of an INCLUDE or APPEND structure. Yes, in that case, they will be missing the the example code. You have to do some recursive type calling of the methods to retrieve the structure of the include structures. Some similar to this. rsbasidoc contains both an INCLUDE structure as well as an APPEND structure, and the fields of these structures are now outputted as well. If your structure has a deeper structure you would have to code for the next level as well. You will probabaly want to come up with some kind of recursive calling here so that you reuse a lot of the example coding.

DATA: ls_struct TYPE rsbasidoc .

DATA:
lr_structdescr TYPE REF TO cl_abap_structdescr,
lr_datadescr TYPE REF TO cl_abap_datadescr,
lr_datadescr_incl TYPE REF TO cl_abap_datadescr,
lt_components TYPE abap_component_tab,
ls_components TYPE LINE OF abap_component_tab,
lt_components_incl TYPE abap_component_tab,
ls_components_incl TYPE LINE OF abap_component_tab.

* now decribe the LS_STRUCT structure
lr_structdescr ?= cl_abap_structdescr=>describe_by_data( ls_struct ).
lt_components = lr_structdescr->get_components( ).
LOOP AT lt_components INTO ls_components.
  lr_datadescr = ls_components-type.
  IF ls_components-as_include = space.
    WRITE:/ ls_components-name, lr_datadescr->type_kind, lr_datadescr->length.
  ELSE.  " IF include or append structure, you need to get that structures components now.
    lr_structdescr ?= ls_components-type.
    lt_components_incl = lr_structdescr->get_components( ).
    LOOP AT lt_components_incl INTO ls_components_incl.
      lr_datadescr_incl = ls_components_incl-type.
      WRITE:/ ls_components_incl-name, lr_datadescr_incl->type_kind, lr_datadescr_incl->length.
    ENDLOOP.

  ENDIF.
ENDLOOP.

Regards,

Rich Heilman

0 Kudos
309

Thanks that wokrs fine. But my next problem is that now I know the attributes from the structure but no her value?

How can I reach the value of every attribute?

0 Kudos
309

Is there no way to read the attribute name and value from a structure in a loop o so on without hardcoded <structureName>-<attributeName> ?

Pls help...

0 Kudos
309

Yes, sure, just use the ASSIGN COMPONENT OF statement.

Regards,

Rich Heilman