2017 Jul 27 7:52 AM
Consider the following code snippet:
TYPES: BEGIN OF ty_tab,
col1 TYPE char20,
col2 TYPE char20,
col3 TYPE i,
col4 TYPE i,
END OF ty_tab.
DATA: it_tab TYPE TABLE OF ty_tab,
wa_tab TYPE ty_tab.
FIELD-SYMBOLS: <fs_tab> TYPE STANDARD TABLE,
<fs_wa> TYPE any,
<fs> TYPE any,
<fs_temp> TYPE any,
<fs_name> TYPE any.
CLEAR wa_tab.
MOVE 'TEST1' TO wa_tab-col1.
MOVE 'TEST2' TO wa_tab-col2.
MOVE 10 TO wa_tab-col3.
MOVE 20 TO wa_tab-col4.
APPEND wa_tab TO it_tab.
CLEAR wa_tab.
MOVE 'TEST5' TO wa_tab-col1.
MOVE 'TEST6' TO wa_tab-col2.
MOVE 30 TO wa_tab-col3.
MOVE 40 TO wa_tab-col4.
APPEND wa_tab TO it_tab.
IF it_tab IS NOT INITIAL.
ASSIGN it_tab TO <fs_tab>.
IF <fs_tab> IS ASSIGNED.
LOOP AT <fs_tab> ASSIGNING <fs_wa>.
ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_wa> TO <fs>.
IF <fs> IS ASSIGNED.
ASSIGN <fs> TO <fs_temp>.
ASSIGN 'WA_TAB-COL3' TO <fs_name>.
>>>>MOVE <fs_temp> TO <fs_name>. " Error line
MODIFY <fs_tab> FROM <fs_wa>.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
Runtime error:
Field "<FS_NAME>" was to assigned a new value but this field is at least partly protected against changes.
I'm trying to assign the value of col1 to col3 via field-symbols. <fs_name> points to wa_tab-col3 as per ASSIGN 'WA_TAB-COL3' TO <fs_name>. @ runtime or so I think. Will it not be possible to change the contents of wa_tab-col3 by altering the value of <fs_name>?
2017 Jul 27 8:39 AM
Ok. I resolved it myself. It was a stupid mistake.
DATA: lv_fname TYPE string.
MOVE 'WA_TAB-COL3' TO lv_fname. " Determined dynamically
ASSIGN (lv_fname) TO <fs_name>.
MOVE <fs_temp> TO <fs_name>.
2017 Jul 27 8:05 AM
Every assign must be accompanied by an Unassign.
If you assign a field to an FS, that exists, and then try to assign it to a field that does not exist the Fs is not unassigned, it is not changed.
So,
Learn good programming practice. Unassign each and every field symbol immediately after you have finished using it. Free every object (using ->Free( ) if it has that), and Free: if not. Free every variable you declare at the end of the procedure.
Do NOT use global variables unless SAP requires them.
IF <fs> IS ASSIGNED.
ASSIGN <fs> TO <fs_temp>.
ASSIGN 'WA_TAB-COL3' TO <fs_name>.
>>>>MOVE <fs_temp> TO <fs_name>. " Error line
MODIFY <fs_tab> FROM <fs_wa>.
UNASSIGN: <fs_temp>, <------------------- New line
<fs>,
<fs_name>.
ENDIF.
2017 Jul 27 8:57 AM
Thanks for the insight on programming practices. I'll definitely incorporate these suggestions from now on.
2017 Jul 27 8:39 AM
Ok. I resolved it myself. It was a stupid mistake.
DATA: lv_fname TYPE string.
MOVE 'WA_TAB-COL3' TO lv_fname. " Determined dynamically
ASSIGN (lv_fname) TO <fs_name>.
MOVE <fs_temp> TO <fs_name>.