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

Dynamic Field type declaration in a structure

Former Member
0 Likes
2,508

Hi All,

How to declare the data element of a field dynamically in the structure i.e, how to define a field type dynamically in the structure?

Please find below example for better understanding of my query and share your ideas if we have a solution for such requirement.

TYPES: BEGIN OF ts_struc,

fld1 TYPE matnr,

fld2 TYPE maktx,

fld3 TYPE mtart,

fld4,

fld5,

fld6,

END OF ts_struc.

DATA: it_struc TYPE STANDARD TABLE OF ts_struc.

PARAMETERS: p_cc RADIOBUTTON GROUP rg1,

p_us RADIOBUTTON GROUP rg1.

if p_cc eq 'X'.

fld4, fld5, fld6 should be of type WOG0XXX.

elseif p_us eq 'X'.

fld4, fld5, fld6 should be of type WKG0XXX.

endif.

Regards,

Shano

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,981

Hi,

If we know the type of fields to be declared( i mean if we are not fetching them dynamically in the prog), we can create the two different structures ty_struct1 and ty_struct2 and then use them in the conditions for the declaration

if p_cc eq 'X'.

*fld4, fld5, fld6 should be of type WOG0XXX.

itab1 type table of ty_struct1.

elseif p_us eq 'X'.

*fld4, fld5, fld6 should be of type WKG0XXX.

itab1 type table of ty_struct2.

endif.

10 REPLIES 10
Read only

Former Member
0 Likes
1,982

Hi,

If we know the type of fields to be declared( i mean if we are not fetching them dynamically in the prog), we can create the two different structures ty_struct1 and ty_struct2 and then use them in the conditions for the declaration

if p_cc eq 'X'.

*fld4, fld5, fld6 should be of type WOG0XXX.

itab1 type table of ty_struct1.

elseif p_us eq 'X'.

*fld4, fld5, fld6 should be of type WKG0XXX.

itab1 type table of ty_struct2.

endif.

Read only

0 Likes
1,981

Hi Deepak,

Thanks for your response. The logic you suggested does not work since system throws a syntax error "itab1 is already declared" as we are trying to declare it again.

Regards,

Shano

Read only

0 Likes
1,981

Hi Shano,

welcome in the world of field symbols:

data:
  lr_itab type ref to table.
 field-symbols:
   <itab1> type table.
if p_cc eq 'X'.
  create data lr_itab type table of ty_struct1.
*fld4, fld5, fld6 should be of type WOG0XXX
elseif p_us eq 'X'.
  create data lr_itab type table of ty_struct2.
*fld4, fld5, fld6 should be of type WKG0XXX.
endif.
ASSIGN lr_itab->* to <itab1>.

Now use <itab1> representing the internal table.

Regards

Clemens

Read only

0 Likes
1,981

Hi Clemens,

Thanks for the response.

data:  lr_itab type ref to table.

I am getting the error "You cannot declare a generic type after "REF TO". ".

Not sure how to get rid of this error since I am new to ABAP OO. Can you please help.

Regards,

Shano

Read only

Shahid
Product and Topic Expert
Product and Topic Expert
0 Likes
1,981

data: dref type ref data.

field-symbols: <fs_str> type WOG0XXX.

CREATE DATA dref TYPE WOG0XXX.

ASSIGN dref->* TO <fs_str>.

now <fs_str> can be used as WOG0XXX.

Edited by: ssm on Jul 28, 2011 4:58 PM

For ex:

DATA: lv_kunnr TYPE kunnr.

DATA: kunnr TYPE REF TO data.

field-symbols: <fs_kunnr> TYPE ANY.

CREATE DATA kunnr TYPE kunnr.

lv_kunnr = '123'.

ASSIGN kunnr->* TO <fs_kunnr>.

move lv_kunnr to <fs_kunnr>.

write: <fs_kunnr>.

Edited by: ssm on Jul 28, 2011 5:00 PM

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,981

Hello Shano,

You got to use the RTTC methods to generate the dynamic structure.

In the COMPONENT_TABLE the rows for the fields fld1-fld3 will be static. And based on the condition mentioned, the type of the columns fld4-fld6 will either be WOG0XXX or WKG0XXX.

There are many threads available on the usage of RTTC methods. Search the forums, google it out & get back to us in case you have any problems using them.

BR,

Suhas

Read only

Shahid
Product and Topic Expert
Product and Topic Expert
0 Likes
1,981

TYPE REF TO data.

See my above example

Read only

Former Member
0 Likes
1,981

Hi ssm and Clemens,

Thanks for your quick response and your help.

Regards,

Shano

Read only

Clemenss
Active Contributor
0 Likes
1,981

shame on me, should know better. Thanks ssm.

Clemens

Read only

Shahid
Product and Topic Expert
Product and Topic Expert
0 Likes
1,981

Clemens, You know lot better and that helps us All.