Dynamic Programming with Field Symbols

Introduction

Field Symbols allows you to access content of variable to which its point to. This pointing to a variable is done using ASSIGN statement. Field Symbol does not store any value it simply point to the memory where variable value is store. Any operation to change value direct affects variable. Field symbols are different in a way that one field symbol can be assigned to different variable throughout the program life time and statement executed using field symbol will affect different variable depending on where it point to.

You can declare field symbols using

FIELD-SYMBOLS : <fs_name> TYPE ANY.
FIELD-SYMBOLS : <fs_name> TYPE mara.
FIELD-SYMBOLS : <fs_name> TYPE i .

You can use ANY to define a generic field symbol which will then inherit properties of variable its assigned to or you can use a specific type to restrict assignment to compatible variable. This means that if you have field symbol defined as I (integer) and you try to assign it to any other kind of variable then you will get ‘type-incompatible’ syntax error.

ASSIGN

Assignment of field symbol is done using ASSIGN statement. Below is an example where I have declared a generic field symbol and assigned it different component of MARA work area.

FIELD-SYMBOLS : <fs_field> TYPE ANY .
DATA : ls_mara TYPE mara .

SELECT SINGLE *
  INTO ls_mara
  FROM mara
 WHERE matnr = 'A1' .

ASSIGN ls_mara-matnr TO <fs_field> .
WRITE : / <fs_field> .

ASSIGN ls_mara-matkl TO <fs_field> .
WRITE : / <fs_field> .
Caution

Accessing field symbol before its assigned to a variable is something which will trigger runtime error ‘Field symbol has not yet been assigned’. An unassigned field symbol cannot be checked when you activate the program and because of this reason programs using field symbol, if checks are not done properly, often produce runtime error.

Whist using field symbols you have to take extra care to check if field symbol points to a variable before you start any operation on it. If you are accessing field symbol ASSIGN statement you must check of assignment has executed successfully.

ASSIGN ls_mara-matnr TO <fs_field> .
IF sy-subrc = 0 .
  WRITE : / <fs_field> .
ENDIF.

or

ASSIGN ls_mara-matnr TO <fs_field> .
IF <fs_field> is ASSIGNED .
  WRITE : / <fs_field> .
ENDIF.
Performance Gain

Field symbols can also be used to access internal table content and it particularly have performance advantage as content of table is not moved to workarea instead field symbol is simply pointed to right record.

DATA : i_mara TYPE TABLE OF mara .
FIELD-SYMBOLS : <fs_mara> TYPE mara .

READ TABLE i_mara ASSIGNING <fs_mara> WITH KEY matnr = 'A1' .
IF sy-subrc = 0 .
  WRITE : <fs_mara>-matnr .
ENDIF.
DATA : i_mara TYPE TABLE OF mara .
FIELD-SYMBOLS : <fs_mara> TYPE mara .

LOOP AT i_mara ASSIGNING <fs_field> .
  WRITE : / <fs_field>-matnr .
ENDLOOP..

Note that if you change content of field symbol after READ internal table statement or within LOOP you are modifying the content of field symbol as there is no work area.

Dynamic Stuff

ASSIGN COMPONENT statement has two variations which allows you to access field of workarea at runtime. In first variantion you can specify field name in terms of number. It’s like saying assign first (or second, third or so on) component of workarea to field symbol. In below code program simply access all component of work area within WHILE loop and exit when assignment fail, which it will once it has accessed all component.

DATA : ls_mara TYPE mara .
FIELD-SYMBOLS : <fs_field> TYPE ANY.

SELECT SINGLE *
  INTO ls_mara
  FROM mara
 WHERE matnr = 'A1' .

WHILE sy-subrc = 0.
  ASSIGN COMPONENT sy-index OF STRUCTURE ls_mara TO <fs_field>.
  IF sy-subrc = 0 .
    WRITE : / <fs_field> .
  ENDIF.
ENDWHILE.

In second variation, which I find more useful, you can specify component name instead of number to access that field of work area.

DATA : ls_mara TYPE mara .
FIELD-SYMBOLS : <fs_field> TYPE ANY.

DATA : lv_compnent_name TYPE char10 VALUE 'MATNR' .

SELECT SINGLE *
  INTO ls_mara
  FROM mara
 WHERE matnr = 'A1' .

ASSIGN COMPONENT lv_compnent_name OF STRUCTURE ls_mara TO <fs_field>.
IF sy-subrc = 0 .
  WRITE : / <fs_field> .
ENDIF.

2 Replies to “Dynamic Programming with Field Symbols

  1. I have a question. I understand that sometimes field-symbols have to be checked if they are assigned. But why should we always check for assignment? In your example.
    ASSIGN COMPONENT lv_compnent_name OF STRUCTURE ls_mara TO .
    will alway be assigned as lv_component_name = MATNR is a component of ls_mara because ls_mara is type mara. The only reason is not assignes could be, the developer changes the type of ls_mara or the value of lv_component_name. And if that happens, the Program logic is destroid and I want a an error in ST22, because there are the most information about the error. That would be my logic

Leave a Reply