2008 May 13 6:23 PM
Hi,
I want something like this:
DATA: BEGIN OF IT_USERS OCCURS 0,
NAME(30),
AGE(2),
END OF IT_USERS,
WA_USERS LIKE LINE OF IT_USERS.
FIELD-SYMBOLS: <DYN_FIELD>.
ASSIGN 'WA_USERS-NAME' TO <DYN_FIELD>.
CLEAR: WA_USERS.
<DYN_FIELD> = 'John'.
APPEND WA_USERS TO IT_USERS.I need to give the name of the fields in runtime, and then assign a value to them.
Can someone help me?
Regards,
Isaac Melendez
2008 May 13 6:28 PM
You can take the field name in the one variable than assing that to the field symbol. It will automatically assign that value to field also.
Like
DATA: BEGIN OF IT_USERS OCCURS 0,
NAME(30),
AGE(2),
END OF IT_USERS,
WA_USERS LIKE LINE OF IT_USERS.
FIELD-SYMBOLS: <DYN_FIELD>.
DATA: L_FIELD(20).
L_FIELD = 'WA_USERS-NAME'.
ASSIGN (L_FIELD) TO <DYN_FIELD>.
* ASSIGN 'WA_USERS-NAME' TO <DYN_FIELD>.
CLEAR: WA_USERS.
<DYN_FIELD> = 'John'.
APPEND WA_USERS TO IT_USERS.
Regards,
Naimesh Patel
2008 May 13 6:28 PM
You can take the field name in the one variable than assing that to the field symbol. It will automatically assign that value to field also.
Like
DATA: BEGIN OF IT_USERS OCCURS 0,
NAME(30),
AGE(2),
END OF IT_USERS,
WA_USERS LIKE LINE OF IT_USERS.
FIELD-SYMBOLS: <DYN_FIELD>.
DATA: L_FIELD(20).
L_FIELD = 'WA_USERS-NAME'.
ASSIGN (L_FIELD) TO <DYN_FIELD>.
* ASSIGN 'WA_USERS-NAME' TO <DYN_FIELD>.
CLEAR: WA_USERS.
<DYN_FIELD> = 'John'.
APPEND WA_USERS TO IT_USERS.
Regards,
Naimesh Patel
2008 May 13 6:29 PM
Hello Isaac.
Do like this:
DATA: lv_var(13) TYPE c VALUE 'WA_USERS-NAME'.
DATA: BEGIN OF it_users OCCURS 0,
name(30),
age(2),
END OF it_users,
wa_users LIKE LINE OF it_users.
FIELD-SYMBOLS: <dyn_field>.
ASSIGN (lv_var) TO <dyn_field>.
CLEAR: wa_users.
<dyn_field> = 'John'.
APPEND wa_users TO it_users.
Best regards.
Valter Oliveira.
2008 May 13 6:30 PM
Check this
DATA: BEGIN OF IT_USERS OCCURS 0,
NAME(30),
AGE(2),
END OF IT_USERS,
WA_USERS LIKE LINE OF IT_USERS.
DATA: V_FIELD LIKE DD03L-FIELDNAME.
FIELD-SYMBOLS: <DYN_FIELD>.
MOVE 'WA_USERS-NAME' TO V_FIELD.
CONDENSE V_FIELD NO-GAPS.
ASSIGN (V_FIELD) TO <DYN_FIELD>.
CLEAR: WA_USERS.
WRITE 'John' TO <DYN_FIELD>.
APPEND WA_USERS TO IT_USERS.
a®
2008 May 13 6:32 PM
2008 May 13 6:35 PM
I've already solved the problem.
I made a mistake. I just need to put it without quotes.
ASSIGN WA_USERS-NAME TO <DYN_FIELD>.Thanks Naimesh and Valter, your answers help me.
Regards,
Isaac Melendez
2008 May 13 6:43 PM
Issac,
I have question
if you are putting without quotes
ASSIGN WA_USERS-NAME TO pointing to ?
what will be the value of WA_USERS-NAME?.
APPEND WA_USERS TO IT_USERS.
a®
2008 May 13 7:17 PM
Hi a®s,
I know is pointless my example, but it's just a test that I'm doing so I can learn how to
handle dynamic fields. I'm not going to put WA_USERS-NAME in the program that I'm
doing, it's just that I want to know how to access to the dynamic field. In the program
that I'm working on I have the name of the fields in an Excel file, and then I put them
in an internal table.
Regards,
Isaac Melendez
2008 May 13 6:42 PM
Your requirement is a little unclear, as if you want a table with name and ages, then you have already defined it correctly, just fill in the values for NAME and AGE. Now if you want a dynanic structure of your internal table, where the column names are the actuall names, and the value of age column is the age, then you can do something like this. Here we are creating the internal table as well as the structure, not sure how you would use the internal table in this case, but you could of course use the structure, each column would be a different person and the the value for that column is the age. See this example.
REPORT rich_0001.
DATA: lo_struct_type TYPE REF TO cl_abap_structdescr.
DATA: lo_table_type TYPE REF TO cl_abap_tabledescr.
DATA: lo_dataref TYPE REF TO data.
DATA: lt_comp_tab TYPE cl_abap_structdescr=>component_table.
DATA: ls_comp_tab LIKE LINE OF lt_comp_tab.
FIELD-SYMBOLS: <lt_table> TYPE STANDARD TABLE,
<ls_table> TYPE ANY,
<lv_field> TYPE ANY.
* Add components
ls_comp_tab-name = 'JOHN'.
ls_comp_tab-type ?= cl_abap_datadescr=>describe_by_name( 'INT4' ).
APPEND ls_comp_tab TO lt_comp_tab.
ls_comp_tab-name = 'BOB'.
ls_comp_tab-type ?= cl_abap_datadescr=>describe_by_name( 'INT4' ).
APPEND ls_comp_tab TO lt_comp_tab.
ls_comp_tab-name = 'SAM'.
ls_comp_tab-type ?= cl_abap_datadescr=>describe_by_name( 'INT4' ).
APPEND ls_comp_tab TO lt_comp_tab.
* Create dynamic internal table based on LT_COMP_TAB
lo_struct_type = cl_abap_structdescr=>create( lt_comp_tab ).
lo_table_type = cl_abap_tabledescr=>create( p_line_type = lo_struct_type ).
* Create references to dyn internal table and work area
CREATE DATA lo_dataref TYPE HANDLE lo_table_type.
ASSIGN lo_dataref->* TO <lt_table>.
CREATE DATA lo_dataref TYPE HANDLE lo_struct_type.
ASSIGN lo_dataref->* TO <ls_table>.
* Fill work area with values.
ASSIGN COMPONENT 'JOHN' OF STRUCTURE <ls_table> TO <lv_field>.
IF sy-subrc = 0.
<lv_field> = '29'.
ENDIF.
ASSIGN COMPONENT 'BOB' OF STRUCTURE <ls_table> TO <lv_field>.
IF sy-subrc = 0.
<lv_field> = '35'.
ENDIF.
ASSIGN COMPONENT 'SAM' OF STRUCTURE <ls_table> TO <lv_field>.
IF sy-subrc = 0.
<lv_field> = '42'.
ENDIF.
* Append this work area to the internal table.
APPEND <ls_table> TO <lt_table>.
CHECK sy-subrc = 0.Regards,
Rich Heilman
2008 May 13 7:33 PM
Hi Rich,
The example that I put was an example of a test that I was doing to know to handle
dynamic fields. In my case I don´t need a dynamic structure, just dynamic fields. But
your example is very helpful. Thanks.
Regards,
Isaac Melendez