✒️ABAP Los Field Symbols
ABAP Los Field Symbols
Field Symbols
A Field Symbol is an ABAP statement that allows dynamic handling of program data during runtime. In contrast to static data access, where we need to specify the name of an object to perform actions on it, Field Symbols enable access to and passing of data whose names and attributes are unknown until runtime.
Field Symbols can be considered symbolic names for data, as when used, the system works with the content of the assigned data, not the Field Symbol itself. Field Symbols do not physically reserve space for a field but rather "point" to its content. A Field Symbol can point to any data object.
Field Symbols provide great flexibility because:
- For processing parts of fields, they allow dynamic specification of offset and displacement.
- They can be forced to take different technical attributes of the assigned field.
Most errors in using Field Symbols are detected only at runtime, making them more challenging to identify. Therefore, Field Symbols should only be used when conventional ABAP statements cannot achieve the same result.
Declaration of a Field Symbol
To use a Field Symbol in an ABAP program, two simple steps must be taken:
- Declare the Field Symbol.
- Assign the data object to the Field Symbol.
The syntax for declaring a Field Symbol is:
FIELD-SYMBOLS <fs> [TYPE <type> | STRUCTURE <s> DEFAULT <wa>].
- If no type is specified, the Field Symbol can contain any data, adopting all technical attributes of the field.
- If a type is specified, the system checks compatibility between the Field Symbol and the associated field at runtime.
Generic data types for a Field Symbol include:
- TYPE ANY or unspecified: Accepts all data object types.
- TYPE C, I, N, P, or X: Accepts only specified data types, adopting length and decimals.
- TYPE TABLE: Checks if the field is a standard table, inheriting all attributes.
- TYPE ANY TABLE: Checks if the field is an internal table, inheriting all table attributes.
- TYPE INDEX TABLE: Checks if the field is an indexed table, inheriting all table attributes.
- TYPE STANDARD TABLE: Checks if the field is a standard table, inheriting all table attributes.
- TYPE SORTED TABLE: Checks if the field is a sorted table, inheriting all table attributes.
- TYPE HASHED TABLE: Checks if the field is a hashed table, inheriting all table attributes.
FIELD-SYMBOLS <fs> TYPE i.
Data Assignment to a Field Symbol
In an ABAP program, data objects can be assigned to Field Symbols at any time, allowing various objects to be assigned to a single Field Symbol. Data assignment to a Field Symbol is done using the ASSIGN statement. The assignment is static since we know the field name we want to assign to the Field Symbol.
The syntax for the ASSIGN statement is:
ASSIGN <field> TO <fs>.
Where <field> is the assigned field and <fs> is the Field Symbol name. After assigning a data object to a Field Symbol, changes in the Field Symbol value also update the corresponding data object value.
DATA: lv_value TYPE i.
FIELD-SYMBOLS <fs> TYPE i.
lv_value = 42.
ASSIGN lv_value TO <fs>.
WRITE: / 'Field Symbol Value:', <fs>. " 42
<fs> = 10.
WRITE: / 'Field Symbol Value:', <fs>. " 10
WRITE: / 'Variable Value:', lv_value. " 10
Using Field Symbols with Structures
Instead of using a structure or work area, we can read a record from an internal table using a Field Symbol. A Field Symbol can be declared in conjunction with an internal table, eliminating the need for declaring a structure or work area. The same Field Symbol can be used to update the internal table.
Considerations
- Modifying any structure field in the Field Symbol updates the corresponding field in the internal table.
- No need for the MODIFY statement, as the Field Symbol directly references the internal table row, making processing faster compared to using a work area or structure.
DATA: it_mara TYPE STANDARD TABLE OF mara.
FIELD-SYMBOLS: <fs_mara> TYPE mara.
READ TABLE it_mara ASSIGNING <fs_mara> WITH KEY matnr = 'MAT1'.
SELECT * FROM mara UP TO 5 ROWS INTO TABLE it_mara.
* Before modification
LOOP AT it_mara ASSIGNING <fs_mara>.
WRITE:/ 'MATNR:', <fs_mara>-matnr, 'MATK1:', <fs_mara>-matkl.
ENDLOOP.
ULINE.
LOOP AT it_mara ASSIGNING <fs_mara>.
<fs_mara>-matkl = 'DEMO'.
ENDLOOP.
ULINE.
* After modification
LOOP AT it_mara ASSIGNING <fs_mara>.
WRITE:/ 'MATNR:', <fs_mara>-matnr, 'MATK1:', <fs_mara>-matkl.
ENDLOOP.
Using Field Symbols for Dynamic SELECT Statement
Field Symbols can be used for dynamic SELECT statements on SAP database tables. This allows creating an ABAP program with a parameter for the desired database table name. Depending on user input, the program can either download table records to a file or upload them to memory for processing.
* Dynamic reference to create a new table structure
CREATE DATA dtable TYPE (tabname).
ASSIGN dtable->* TO <fs_itable>.
* Fetch the data from the selected table
SELECT *
FROM (tabname) UP TO 3 ROWS
INTO <fs_itable>.
ENDSELECT.
E.g 1
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_25
*&---------------------------------------------------------------------*
*& This report demonstrates dynamic selection and display of data from
*& different tables based on user selection.
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_25.
CONSTANTS: c_x(1) TYPE c VALUE 'X'.
DATA: dtable TYPE REF TO data, " Reference to dynamic data
tabname TYPE tabname. " Table name for dynamic selection
FIELD-SYMBOLS: <fs_itable> TYPE any, " Field symbol for table row
<fs_value> TYPE any. " Field symbol for row value
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-t01.
PARAMETERS: p_eban RADIOBUTTON GROUP rad1, " Radio button for table EBAN
p_mara RADIOBUTTON GROUP rad1, " Radio button for table MARA
p_vbak RADIOBUTTON GROUP rad1. " Radio button for table VBAK
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
CLEAR tabname.
* Determine table name based on user selection
IF p_eban EQ c_x.
tabname = 'EBAN'. " Selected table is EBAN
ELSEIF p_mara EQ c_x.
tabname = 'MARA'. " Selected table is MARA
ELSEIF p_vbak EQ c_x.
tabname = 'VBAK'. " Selected table is VBAK
ENDIF.
PERFORM select_data. " Perform data selection
PERFORM display_data. " Perform data display
*---------------------------------------------------------------------*
* Form: SELECT_DATA
*---------------------------------------------------------------------*
*& This form selects data from the dynamically determined table
*---------------------------------------------------------------------*
FORM select_data.
* Dynamic reference to create a new table structure
CREATE DATA dtable TYPE (tabname).
ASSIGN dtable->* TO <fs_itable>.
* Fetch the data from the selected table
SELECT *
FROM (tabname) UP TO 3 ROWS
INTO <fs_itable>.
ENDSELECT.
ENDFORM. " SELECT_DATA
*---------------------------------------------------------------------*
* Form: DISPLAY_DATA
*---------------------------------------------------------------------*
*& This form displays the selected data
*---------------------------------------------------------------------*
FORM display_data.
ULINE.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_itable> TO <fs_value>.
IF sy-subrc <> 0.
EXIT. " No more data
ENDIF.
WRITE:/ sy-index, ' ' , <fs_value> , ' - '.
ENDDO.
ENDFORM. " DISPLAY_DATA
E.g 2
DATA: it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.
FIELD-SYMBOLS: <fs_it_mara> TYPE mara,
<fs_value> TYPE any.
SELECT * FROM mara UP TO 10 ROWS INTO TABLE it_mara.
* Iterating the internal table
LOOP AT it_mara ASSIGNING <fs_it_mara>.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_it_mara> TO <fs_value>.
IF sy-subrc = 0.
WRITE: / <fs_value>.
ENDIF.
ASSIGN COMPONENT 'MTART' OF STRUCTURE <fs_it_mara> TO <fs_value>.
IF sy-subrc = 0.
WRITE: / <fs_value>.
ENDIF.
ULINE.
ENDLOOP.
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 102 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez