Application Development and Automation 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: 
Read only

create new fields dynamically in nested structure

Former Member
0 Likes
2,820

Hi,

i have nested structure.

like structure A

feild1

field2

field3 type structure B,---->nested

and structure B,

field4 type structure C.------>nested

structure c,

field5

i wanted to add 3 new fields dynamically to structure C, so finally structure A ,B should be same but structure C ,should only should contain additional fields.

Can anyone help me out. What i did till now is getting the components and then appending 3 new fields . but this 3 new fields are reflected as feilds of structure A, where as i need at structure C level.

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
2,616

You can't change structure C without affecting A and B as long as they inlcude the former as their substructure. What I suggest is to get components of C, append new fields and generate new separate structure without affecting any of the above.

Regards

Marcin

18 REPLIES 18
Read only

Former Member
0 Likes
2,616

tHIS IS THE STRUCTURE I AM USING IN MY PROGRAM.

Types: begin of ty_struct3,

f5 type string,

f6 type string,

end of ty_struct3.

types:begin of ty_struct2,

f4 type ty_struct3,

end of ty_struct2.

Types; begin of ty_struct1,

f1 type string,

f2 type string,

f3 type ty_struct2,

end of ty_struct1.

Final structure should contains two additional fields after f5,f6. of ty_struct3.

Read only

MarcinPciak
Active Contributor
0 Likes
2,617

You can't change structure C without affecting A and B as long as they inlcude the former as their substructure. What I suggest is to get components of C, append new fields and generate new separate structure without affecting any of the above.

Regards

Marcin

Read only

0 Likes
2,616

ok i will try.

Read only

0 Likes
2,616

Hi,

Can you post the code please?

Read only

0 Likes
2,616

Don't think you can change original structure dynamically. It will be rather creating new one based on old one instead.


DATA: BEGIN OF gs_c,
        field1 TYPE c,
        field2 TYPE n,
      END OF gs_c.

DATA: gr_structdescr TYPE REF TO cl_abap_structdescr,
      gr_elemdescr   TYPE REF TO cl_abap_elemdescr.
DATA gt_components TYPE cl_abap_structdescr=>component_table WITH HEADER LINE.

DATA: gr_new_str TYPE REF TO data.

FIELD-SYMBOLS <new_str> TYPE ANY.

gr_structdescr ?= cl_abap_typedescr=>describe_by_data( gs_c ).
gt_components[] = gr_structdescr->get_components( ).

"add new field3 of type I
gr_elemdescr = cl_abap_elemdescr=>get_i( ).
gt_components-name = 'FIELD3'.
gt_components-type = gr_elemdescr.
APPEND gt_components.

gr_structdescr = cl_abap_structdescr=>create( gt_components[] ).
CREATE DATA gr_new_str TYPE HANDLE gr_structdescr.
ASSIGN gr_new_str->* TO <new_str>.

Regards

Marcin

Read only

0 Likes
2,616

Hi Marcin ,

Thats true. This is adding aadditional fields dynamically to one structure..

So it means the requirement which i have is not feasible at all.?

Read only

0 Likes
2,616

Yes, I doubt this is feasible at all.

Regadrs

Marcin

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,616

Hello Marcin & Sreenu,

AFAIK changing the structure of an existing TYPE is not possible. As mentioned you can get the components of the current structure & create a new one dynamically from it. I remember similar discussion in the forum long back.

Watsay Marcin?

BR,

Suhas

Read only

0 Likes
2,616

Hi Suhas,marcin,

Thanks a lot.

Could you tell me whether my below query is possible or not ?

lets think there is no structure at all. But at some place in my program i wanted to create the structures as mentioned above dynamically.

I have seen a article which creates complex structures at run time and also fills the values

This is the link.

https://wiki.sdn.sap.com/wiki/display/Snippets/Creating%20Flat%20and%20Complex%20Internal%20Tables%2...

BR

Sreenu

Read only

0 Likes
2,616

If you want to create dynamically such nested strctues please refer my blog [Do you really know everything about typing? - part 2|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/20887] [original link is broken] [original link is broken] [original link is broken];. It describes the concept from scratch.

Regards

Marcin

Read only

0 Likes
2,616

Hi MARCIN,

Real thanks for the document. I will try my best to understand and work out with that.

Read only

0 Likes
2,616

HI MARCIn,

Can you help me out with the code? because when i am trying i am not getting the actual structure. I know i am missing many things . So please can post the code.

Regards

Sreen

Read only

0 Likes
2,616

HI MARCIn,

Can you help me out with the code? because when i am trying i am not getting the actual structure. I know i am missing many things . So please can post the code.

Regards

Sreen

Read only

0 Likes
2,616

HI MARCIn,

Can you help me out with the code? because when i am trying i am not getting the actual structure. I know i am missing many things . So please can post the code.

Regards

Sreen

Read only

0 Likes
2,616

Hi MARCIN,

I tried to create nested one as given in the documents but could not succeed.

ANyone one help me creating a final structure as given below dynamically.

Actually i need to generate an XML and i require this final structure in exact this way .

types: begin of ty_msg,

f3 ,

f4,

f5,

end of ty_msg.

types: begin of ty_payload,

msg type ty_msg,

end of ty_payload,

This is my final structure

types: begin of ty_final,

f1 ,

f2,

payload type ty_payload,

end of ty_final.

Reagrds

Sreenu

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,616

Hello Sreenu,

In the code snippet i'm attaching i'll try to create a nested structure(as shown below) dynamically:

TYPES:
BEGIN OF zc,
  f5 TYPE char4,
  f6 TYPE char4,
  f7 TYPE char4,
END OF zc,

BEGIN OF zb,
  f4 TYPE zc,
END OF zb,

BEGIN OF za,
  f1 TYPE char4,
  f2 TYPE char4,
  f3 TYPE zb,
END OF za.

Code snippet:

* Step 1: Fill the COMPONENTS table for the innermost structure & move
* upwards (i.e., in this sequence ZC -> ZB -> ZA)

* Step 1.1: Create the innermost structure(i.e., ZC)
DATA: it_comp TYPE cl_abap_structdescr=>component_table,
      wa_comp TYPE cl_abap_structdescr=>component,
      go_elem TYPE REF TO cl_abap_elemdescr.

TRY .
    go_elem = cl_abap_elemdescr=>get_c( 4 ).
  CATCH cx_parameter_invalid_range.
ENDTRY.

wa_comp-name = 'F5'.
wa_comp-type = go_elem. "f5 TYPE char4
APPEND wa_comp TO  it_comp.
CLEAR wa_comp.

wa_comp-name = 'F6'.
wa_comp-type = go_elem. "f6 TYPE char4
APPEND wa_comp TO  it_comp.
CLEAR wa_comp.

wa_comp-name = 'F7'.
wa_comp-type = go_elem. "f7 TYPE char4
APPEND wa_comp TO  it_comp.
CLEAR wa_comp.

DATA: go_struc_zc TYPE REF TO cl_abap_structdescr.

TRY .
    go_struc_zc = cl_abap_structdescr=>create( it_comp ).
    CLEAR it_comp.
  CATCH cx_sy_struct_creation.
ENDTRY.

* Step 1.2: Move one step up & create the next structure(i.e., ZB)
wa_comp-name = 'F4'.
wa_comp-type = go_struc_zc. "f4 TYPE zc
APPEND wa_comp TO it_comp.
CLEAR wa_comp.

DATA: go_struc_zb TYPE REF TO cl_abap_structdescr.

TRY .
    go_struc_zb = cl_abap_structdescr=>create( it_comp ).
    CLEAR it_comp.
  CATCH cx_sy_struct_creation.
ENDTRY.

* Step 1.3: Move one step up & create the next structure(i.e., ZA)
wa_comp-name = 'F1'.
wa_comp-type = go_elem. "f1 TYPE char4
APPEND wa_comp TO  it_comp.
CLEAR wa_comp.

wa_comp-name = 'F2'.
wa_comp-type = go_elem. "f2 TYPE char4
APPEND wa_comp TO  it_comp.
CLEAR wa_comp.

wa_comp-name = 'F3'.
wa_comp-type = go_struc_zb. "f3 TYPE zb
APPEND wa_comp TO it_comp.
CLEAR wa_comp.

DATA: go_struc_za TYPE REF TO cl_abap_structdescr.

TRY .
    go_struc_za = cl_abap_structdescr=>create( it_comp ).
    CLEAR it_comp.
  CATCH cx_sy_struct_creation.
ENDTRY.

* Step 2: Use the RTTC object generated to get the dynamic nested struc

* Step 2.1: Create the dynamic data object based on the RTTC object
DATA: do_nested_struc TYPE REF TO data.
CREATE DATA do_nested_struc TYPE HANDLE go_struc_za.

* Step 2.2: Dereference the data object
FIELD-SYMBOLS <st_nested> TYPE ANY.
ASSIGN do_nested_struc->* TO <st_nested>.

You can check the structure <ST_NESTED> in the debugger & see that it is identical to the statically defined structure ZA.

BR,

Suhas

BR,

Suhas

Read only

0 Likes
2,616

Hi SUHAS,

Thanks a lot..Its great.No words man.

Could you help me in filling the values and appending to an itnernal table. You have helped me in my last post.

But here i am not able to fill vales in the nested structure 'ZC'.

I finally need to append an internal table of type 'ZA'..

Regards

Sreenu

Read only

0 Likes
2,616

Hi Suhas,

A real thanks . i really appreciate your help. I am able to populate the values in the nested structure and finally able to append to internal able as needed.

Thanks to MARCIN also for providing such good document link

I never did dynamic programming till i got the requirement. And you guys helped me all the way with code .

Thanks for helping.keep it up

Regards

Sreenu