‎2011 Jul 28 12:19 PM
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
‎2011 Jul 28 12:27 PM
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.
‎2011 Jul 28 12:27 PM
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.
‎2011 Jul 28 12:46 PM
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
‎2011 Jul 28 1:49 PM
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
‎2011 Jul 28 2:02 PM
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
‎2011 Jul 28 12:27 PM
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
‎2011 Jul 28 1:04 PM
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
‎2011 Jul 28 2:04 PM
‎2011 Jul 28 2:15 PM
Hi ssm and Clemens,
Thanks for your quick response and your help.
Regards,
Shano
‎2011 Jul 29 9:19 AM
‎2011 Jul 29 9:33 AM