‎2014 May 06 1:47 PM
Hi Gurus,
my need is to declare a variabile of a type stored in another string variable. Eg:
DATA: my_type(50) TYPE C.
my_type = 'MATNR'.
Now I want to declare another variable my_var of type MATNR (I fill the variable my_type at runtime)
Any ideas?
Thank you
Francesco
‎2014 May 06 2:03 PM
Answer by Susmitha is the way to do it. Further part of accessing it using field symbols is described below :
Creating variable dynamically :
DATA my_var TYPE REF TO DATA.
CREATE DATA my_var TYPE (my_type).
Accessing the dynamically created variable :
FIELD-SYMBOLS <fs_my_var> TYPE (my_type). "(or u can use TYPE ANY)
ASSIGN my_var->* TO <fs_my_var>.
Assigning values :
<fs_my_var> = xyz-matnr.
WRITE <fs_my_var>.
Regards,
Ashish
‎2014 May 06 1:53 PM
You can dynamically create data using data reference.
data dyn_field type ref to data.
DATA: my_type TYPE string.
my_type = 'MATNR'.
create data dyn_field type (my_type).
You then access it using field symbols.
Check resources on dynamic data declarations, you will find plenty of them.
‎2014 May 06 2:03 PM
Answer by Susmitha is the way to do it. Further part of accessing it using field symbols is described below :
Creating variable dynamically :
DATA my_var TYPE REF TO DATA.
CREATE DATA my_var TYPE (my_type).
Accessing the dynamically created variable :
FIELD-SYMBOLS <fs_my_var> TYPE (my_type). "(or u can use TYPE ANY)
ASSIGN my_var->* TO <fs_my_var>.
Assigning values :
<fs_my_var> = xyz-matnr.
WRITE <fs_my_var>.
Regards,
Ashish
‎2014 May 06 2:36 PM
Thank you very much, now an additional question: I need to declare a type like this:
TYPES:
BEGIN OF gt_s_data2,
my_var
FIELD1 TYPE c,
FIELD2 TYPE c,
END OF gt_s_data2.
where "my_var" is the variable dinamically declared before. How can I achieve this?
Thank you
Francesco
‎2014 May 06 2:46 PM
‎2014 May 06 3:14 PM
Hi Francesco,
There is a function module for that that should always be used:
CONVERSION_EXIT_MATN1_INPUT
That domain MATNR, as several others, has a conversion rule, namely MATN1. All fields with a conversion rule when read from a char like field should be converted to SAP internal format using funtion CONVERSION_EXIT_xxxxx_INPUT.
I guess this is not the point, so my suggestion to create your dynamic variable is to do the following:
DATA: l_type(30),
l_text(10) VALUE '123'.
DATA: BEGIN OF ls_data,
lr_field TYPE REF TO data,
field1 TYPE c,
field2 TYPE c,
END OF ls_data.
FIELD-SYMBOLS <l_field> TYPE any.
l_type = 'MATNR'.
CREATE DATA ls_data-lr_field TYPE (l_type).
ASSIGN ls_data-lr_field->* TO <l_field>.
<l_field> = l_text.
you could use the create structure only part suggested by Susmitha, but if you're not displaying this data it might not be necessary.
regards,
Edgar