Skip to Content

Dynamic Structure creation and assigning data.

Hi,

I have a structure and a work area,

DATA: begin of sample_struct,

elem1 type xxx,

elem2 type yyy,

elem3 type zzz,

end of sample_struct.

DATA: wa_sample_struct type sample_struct.

DATA: lr_rtti_struc TYPE REF TO cl_abap_structdescr.

DATA: lt_comp TYPE cl_abap_structdescr=>component_table.

DATA: ls_comp LIKE LINE OF lt_comp.

DATA: l_index TYPE string.

DATA: lr_data TYPE REF TO data.

DATA: lr_table TYPE REF TO

I create a new structure at run time extending the sample_struct to add few more components at run time like this.

lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data(wa_sample_struct).

lt_comp = lr_rtti_struc->get_components( ).

do 10 times.

l_index = sy-index

concatename 'element' l_index into ls_comp-name

ls_comp-type = cl_abap_elemdescr=>get_string( ).

APPEND ls_comp TO lt_comp.

enddo.

Then i create a work area of this new structure like this.

lr_rtti_struc = cl_abap_structdescr=>create( p_components = lt_comp ).

CREATE DATA lr_data TYPE HANDLE lr_rtti_struc.

ASSIGN lr_data->* TO <fs_data>.

Now <fs_data> is a work area for the structure.

I can move the data in wa_sample_struct into <fs_data> like this.

move-corresponding wa_sample_struct to <fs_data>.

My problem is how do i access the dynamic components of this work area so that i can assign data to those components.

If anybody knows how to access the dynamically added components of <fs_data> so that I can assgin data to those.

Thanks in advance

Sesh

Message was edited by: Seshatalpasai Madala

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

6 Answers

  • Best Answer
    Posted on Mar 07, 2006 at 04:40 AM

    Hi,

    Looks like the problem is not well stated as I am getting answers which are not relevent.

    My existing structure.

    DATA: begin of sample_struct,

    elem1 type xxx,

    elem2 type yyy,

    elem3 type zzz,

    end of sample_struct.

    I create a new structure using this structure at runtime by adding new fields which are in a internal table. I add the new fields as follows.

    loop at it_tab into wa_tab.

    ls_comp-name = wa_tab.

    ls_comp-type = cl_abap_elemdescr=>get_string( ).

    APPEND ls_comp TO lt_comp.

    endloop.

    This internal table is built using the user input. So I dont know how many and what are the values in the internal table.

    LT_COMP has the existing structure field's as well as the newly added fields for my new structure.

    I create a new structure using this component list LT_COMP.

    DATA: lr_rtti_struc TYPE REF TO cl_abap_structdescr.

    lr_rtti_struc = cl_abap_structdescr=>create( p_components = lt_comp ).

    Now I have an object of cl_abap_structdescr. Which I can use as a data type for defining Workarea and internal table of this structure like this.

    CREATE DATA lr_data TYPE HANDLE lr_rtti_struc.

    ASSIGN lr_data->* TO <fs_data>.

    I can access all the existing fields of the strucutre like this.

    <fs_data>-elem1

    <fs_data>-elem2

    My problem is how do i access the newly added fields of the strucutre as I dont know the names of the fileds(I know them only at runtime).

    I hope I have explained it well enough, hope to see a relevent answer for the above problem.

    Thanks in advance.

    Sesh

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 06, 2006 at 12:28 PM

    FIELD-SYMBOLS: <l_field> TYPE ANY TABLE,

    loop at the table which holds the field names.

    ASSIGN COMPONENT table-fieldname OF STRUCTURE <fs_data> TO <l_field>.

    <l_field> = 'Value' .

    endloop .

    table-fieldname is the field which hold the field name in the itab.

    Regards

    Raja

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Mar 06, 2006 at 12:26 PM

    Hi

    You can try to use the internal table where you've defined the dynamic structure: lt_comp (?).

    field-symbols: <fs_value>.

    LOOP AT LT_COMP INTO ls_comp.

    ASSIGN COMPONENT LS_COMP-NAME OF STRUCTURE <fs_data> TO

    <FS_VALUE>.

    ENDLOOP.

    Max

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 06, 2006 at 12:45 PM

    The problem is I dont know how many fields will be added to my structure. And i dont know the name of the fields that will be added to the structure.

    That is my initial strucuture will have follwing fields.

    elem1

    elem2

    elem3

    Once i extend my structure it will have

    elem1

    elem2

    elem3

    xxx (Name of this is not known cannot hardcode this in the code).

    yyy

    zzz

    So I dont know that how many fields are there in my final structure and what are the names of the fields. These field name are choosen by the person who is executing this program. So he can choose to add as many fields as he wants and the name of the fields are choosen by him and i will get them as input to my program.

    <fs_data> is pointing to a workarea of this structure.

    I can accees <fs_data>-elem1 as i know that this structure has this field.

    But i dont know the fields that are added until the runtime so i cannot hardcode.

    Only thing i know is my structure has some new fields.

    IS there some why of getting reference to the newly added field.

    Thanks for the quick responses.

    Sesh

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 06, 2006 at 12:29 PM

    Hi,

    you need a fieldcatalog with all the fields of your structure.

    loop at it_fieldcatalog
      into is_fieldcatalog.
      assign component is_fieldcatalog-fieldname of <fs_data> to <field>.
      assign component is_fieldcatalog-fieldname of is_data to <field_2>.
      move <field> to <field_2>.
    endloop.
    

    Rgd

    Frédéric

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 06, 2006 at 12:50 PM
    • Defining a Field Symbol

    FIELD-SYMBOLS <FS>.

    • Variable for later use

    DATA FIELD VALUE 'X'.

    • Assigning a field to a Field Symbol

    ASSIGN FIELD TO <FS>.

    • Using a Field Symbol which has an assigned field

    WRITE <FS>.

    regards

    vinod

    Add comment
    10|10000 characters needed characters exceeded