SAP ABAP Field Symbols Unleashed

0
3992

SAP ABAP Field Symbols Unleashed

There has been requirements in ABAP to read the data from tables dynamically, based on specific field names. Those are the cases where we don’t know the field name to be read until run-time, and then we are have to read its data too.

The most efficient way to deal with such situations is Field Symbol. As we know, Field Symbols are like a placeholder or symbolic names for any object, variable or value.

SAP ABAP Field Symbols Unleashed

We can use following code as an sample code and customize as per our requirements.

Code snippet:

FIELD-SYMBOLS:
<fieldname> TYPE ANY, “Name of field
<line> TYPE ANY, “Line structure for table
<fieldvalue> TYPE ANY. “Value of field

DATA: l_shorttext LIKE dd03m-ddtext.

DATA: wa_dd03l LIKE dd03l.

SELECT *
FROM dd03l
INTO wa_dd03l
WHERE tabname = ‘ZTEST_TAB’.

* Assign name of field
ASSIGN COMPONENT 2 OF STRUCTURE wa_dd03l TO <fieldname>.

* Assign structure for internal table
ASSIGN wa_pa1_delta TO <line>.

* Assign field value from structure
ASSIGN COMPONENT <fieldname> OF STRUCTURE <line> TO <fieldvalue>.

* Find DD text for field
SELECT SINGLE ddtext
FROM dd03m
INTO l_shorttext
WHERE
tabname = ‘ZTEST_TAB’ AND
fieldname = <fieldname> AND
ddlanguage = ‘EN’.

WRITE: / <fieldname>, l_shorttext, ‘ : ‘, <fieldvalue>.

ENDSELECT.

 

Another code snippet:

Suppose we want to sum the fields from table(say, GLT0) only till the period the user enters on screen.

SELECT * FROM GLT0 WHERE BUKRS = P_BUKRS
AND RYEAR = CURRY
AND RBUSA IN P_GSBER.
V_VAR = 01. ” Temp var for reading Period from 01
WHILE V_VAR <= P_MONAT. ” P_MONAT-> user enters on screen
CONCATENATE ‘TSL’ V_VAR INTO V_WAR. ” concat fieldname into local var
ASSIGN V_WAR TO <FS>. ” Assign to Field Symbol
ASSIGN GLT0 TO <L>. “Assign line/structure
ASSIGN COMPONENT <FS> OF STRUCTURE <L> TO <FS1>. “read fieldvalue
V_CURSTK = V_CURSTK + <FS1>. “adding the values in var
V_VAR = V_VAR + 1. “increment the loop variable
ENDWHILE.
ENDSELECT.

 

LEAVE A REPLY