cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic Structure creation and assigning data.

0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

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

hymavathi_oruganti
Active Contributor
0 Kudos

u can access like this using index number, hope u know index number of newly added fields

IDX = (INDEX NUMBER)

ASSIGN COMPONENT IDX OF STRUCTURE <FS_DATA> TO <FS>.

<FS> = (VALUE).

Former Member
0 Kudos

Seshu,

You can do it two ways.

Assign component sy-index of structure to <fs_any>.

However, in this case it will start from column 1 and there are no names for the columns.

Second way is to use the names that the user has given. As you are getting the input from the user, I am sure you storing it some where.

Use the following statement

Assign component (ls_comp-name) of structure <fs_data> to <fs_any>.

I have used the same wrokarea name where you are getting the field name.

<fs_data> is the work area and <fs_any> is declared as type ANY.

Hope this solves the issue.

Regards,

Ravi

Note : Please reward the helpful posts.

Message was edited by: Ravikumar Allampallam

Former Member
0 Kudos

Hi guys,

how can I dynamically assign a field value to a structure.

for example I have a structure AA with fields A, B and C.

I have a UI to let user to provide data to the structure, the user types in hte field name and the value. Then in my program I have to assign the field dynamically based on hte user's input (field name and value as string). How can I do it? per say, the opposite of "

Assign component (ls_comp-name) of structure <fs_data> to <fs_any>"

Anybody can help?

Thanks

Jianjun

0 Kudos

HI,

Folllowing is the procedure.

Supposing

Parameters: Field_name (User input for field name).

Parameters: Field_value (User input for field value).

You need following data.

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.

You want to add this new field in to the structure 'AA' if i am right?

DATA: wa_aa like AA.

The procedure to do it is,

lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( wa_aa ).

lt_comp = lr_rtti_struc->get_components( ).

ls_comp-name = Field_name.

ls_comp-type = cl_abap_elemdescr=>get_string( ).

APPEND ls_comp TO lt_comp.

CALL METHOD cl_abap_structdescr=>create

EXPORTING

p_components = lt_comp

RECEIVING

p_result = lr_rtti_struc.

CREATE DATA lr_data TYPE HANDLE lr_rtti_struc.

ASSIGN lr_data->* TO <fs_data>.

Now <fs_data> points to the data of new structure.

then use

ASSIGN COMPONENT Field_name OF STRUCTURE <fs_data> TO <fs_val>.

<fs_val> = Field_value.

Hope this solves your problem. Please assign points if the answer is helpful.

Regards,

Sesh

Message was edited by: Jianjun Xiao

Message was edited by: Seshatalpasai Madala

Former Member
0 Kudos

Hi Sesh,

thanks for the hints. I want to give you some points, but don't know how. The instruction from the notification is not accurate and doesn't work.

Thanks any way.

Jianjun

0 Kudos

Hi Jianjun,

As the topic was initiated by me you cannot assign points to me.

Regards,

Sesh

Answers (5)

Answers (5)

athavanraja
Active Contributor

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

vinod_gunaware2
Active Contributor
0 Kudos
  • 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

0 Kudos

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

Former Member
0 Kudos

Hi,

Pls check this... it will continue in the do loop as many times as the components of the dynamic structure...

Do.

assign component sy-index of <fs_data> to <fs_val>.
if sy-subrc <> 0.
  exit.
endif.

Enddo.

Also pls check this thread...

Thanks,

Renjith

FredericGirod
Active Contributor
0 Kudos

For the class CL_ABAP_STRUCTDESCR you have the method GET_DDIC_FIELD_LIST.

Rgd

Frédéric

FredericGirod
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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