Professional Documents
Culture Documents
Field Symbols and Data Reference Variables
Field Symbols and Data Reference Variables
1. Field Symbols
Field symbols can be considered as the symbolic name for the data objects. They do not
physically reserve any memory space when we declare them. They only point to an existing
data object by using Value semantics. Which means, when we access the field symbol, we
are actually working with the contents of the data object which is pointed by the field
symbol, and not the content of the field symbol itself. In order to access the content of the
data object by using the field symbol, we need to assign that data object to the field symbol
first.
1.1.
It is actually not possible to assign a data object to another data object dynamically as
below.
The statement in the line 15 will be the cause the of the error. Remember that the dynamic
values that we give in the braces are not syntax checked and that is the reason why we do
not get any error when we check the syntax before running. It will be checked only during
the run time. Here since, the target of the assignment statement in the line is an integer
variable, it is expecting the source to be an integer, or it will try to interpret the source as a
number. However, the dynamic data object access is not supported here and that is the
reason why it is giving an error.
However, the piece of the code below will work without any issue.
Here we have a field symbol <fs_var2>. To this field symbol we are assigning a data
object dynamically. This is one of the advantages of the field symbols. After this assignment,
we can work with the field symbol just as how we would with the original data object. So
the above code will print the value 90.
Please refer the debugger snap shown below. The control has reached the line 15 , which
means the field symbol is assigned with the original data object. Now, on the right hand
side, both the original data object (lv_var1) and the field symbol (<fs_var2>) are
showing the same value.
Now, change the content of the field symbol dynamically in the debugger itself and you can
see that the content of the original data object is also changed.
Click on the pencil icon near the field symbol for enabling editing.
1.2.
The keyword field-symbols is used to define a field symbol. The angular brackets
(< and > ) are the part of the field symbol definition.
Example is given below.
Ex : If we assign a reference variable to a field symbol, we will not be able to access the
attributes of the objects using the field symbol. Please refer the screenshot below.
Another example, if we assign an internal table to a field symbol, it will not get the keys of
the internal table. In this case we need to dynamically specify the key in brackets.
If you assign a structure( work area) to a field symbol of the generic type, you will not be
able to access the components of the structure by using the operator as we would do
with the original structure variable. However, you can access the individual component of
the field symbol by assigning them to another field symbol. This is discussed in detail later.
When we assign a data object dynamically to the field symbol, the sy-subrc will be set 0 if
the assignment was successful. Otherwise it will be set to 4. So, it is always recommended
that we check the sy-subrc value immediately after the assignment. Otherwise, the field
symbol may refer to an old value.
You can also use offset and length addition to the data object when assigning them to
the field symbol. However this is not possible when assigning a data object dynamically.
Please find the example in the below code.
1.3.
If you want to loop an internal table into a work area and process each records, field
symbols can be found very helpful here.
The above example shows how we can use field symbols for processing individual lines of
the internal table. Here the performance is drastically improved when compared to the
similar operation by using the work area or the header line of the table. This is because the
individual contents of the internal table is physically not moved around as in the case of the
work area. The field symbol is pointing to the individual records of the internal table and for
every loop pass it will point to the next record in the internal table.
Here we are not writing any modify statement itself. When we change the content of the
field symbol, the content of the record to which it is pointing in the internal table itself is
also changed. Here we do not have to check if the field symbol is assigned. This is because if
there is no records in the internal table, that will not enter the loop itself.
This operation is useful when we are working with the internal table which are in the form
of a field symbols.
Most of the standard SAP methods which will return an internal table will always return it in
the form of areference variable. First we need to dereference them into a field
symbol. Remember that the generically types data reference variables can only be
dereferenced by the use of a field symbol. (This is discussed in detail later)
Here the component can be specified by giving the position number of the structure. We
can also use a variable which has the name of the component instead. This offers a great
advantage when it comes to the dynamic access of the components of the structure whose
names are not known till the run time.
1.4.
The statement which we can use in order to check if the field symbol is assigned to a data
object is is assigned.This will be true if the field symbol is assigned to a data object and
false if it is not assigned. Also, we can explicitly unassign a field symbol from a data object by
using the unassign keyword.
Data reference variables are the equalant of the pointers in other languages like C. As you
guess, it is the data object which will hold the address of other data object. So, when we
access the content of a data reference variable, we are accessing the address of another
data object. In order to access the contents of the data object which is pointed by the data
reference variable, a special technique called dereferencing has to be done. So, it is told
that the data reference variable uses reference semantics to work with data objects.
One difference between the pointers in other language and the data reference variable is
that, you will not be able to increment or decrement them to pint to a next address
location.
We need to use the keyword get reference of in order to assign the address of any data
object to the data reference variable. After this process, for accessing the actual content of
the data object using the data reference variable, we use the dereferencing operator ( >* ). The example code will print the below output.
Here we have declared a generic data reference variable. And we used the create
data statement to create it. This statement will create a data object in the memory and
assign the address of the data object to the data reference variable lr_data. The original
name of the data object that was created by this statement is not known and it is of no
interest. In this method, we will be able to use the incomplete type like c and n with the
length specification as well. However will not be able to give a default value by adding
the value keyword. We can also use the like keyword to refer to an object already existing.
The memory for the data object is not assigned till the create data statement. We can even
declare the data reference variable with internal table type as below.
However, one difference of using this method is that we will not be able to work with the
contents of the created data object by using only the dereferencing
operator ->*. This operator cannot be used with the data reference variable of generic
type as a rule. So, we need to assign such variables to a field symbol explicitly and then
we can work with the field symbol seamlessly as we work with the original data object.
In the above code, we have created a data object(internal table) of type sflight by using
the generic data reference variable lr_data. Now, in order to access the contents of the
internal table itself, we need to assign it to a field symbol as in the line 46. Please see the
debugger snapshot, where we can see the field symbol<fs_table> is populated with the
line type of sflight.
Here the type of the data object is given in the braces dynamically. This must be
a character data object. This is one big advantage of the data reference variable. Note
that we will not be able to use like keyword when we declare the data objects dynamically
as shown above. So, using this method, we are able to create data objects whose type and
properties are not known to us in the beginning.
7. Some Scenarios
7.1. Making an Interface parameter generic.
Imagine that we have a function module or a method which should take an internal table
whose type we do not know till the run time. In this case, we need to have a generic type as
the importing parameter of the function module or the method. We can have a generic data
reference variable of type data as the importing parameter here. Please refer the method in
the screen shot below.
We have an importing parameter im_table which is a generic data reference variable. Now,
imagine that you need to call those method from a report and pass an internal table to it.
You can see that we have populated an internal table with values. We are taking the
reference of this internal table to the data reference variable lr_data and passing to the
interface of the generic_int method of the zcl_utilities class. Now, inside this method, as we
know we need to have a field symbol to access the contents of the internal table.
So, the internal table is successfully passed to this method through the generic importing
parameter im_table.Like this, it can take any data object whose type are not known to us.
We just need to assign this to a field symbol for accessing the actual content of it. Now
imagine that we need to print the internal table values from this method. How do we do
that?
The internal table is already available in the form of the field symbol <fs_table>. We
need to loop it record by record to a work area and then print it. We can declare another
field symbol as work area. This will again be a generic one since we do not know the type of
the internal table till the run time. Please refer the code below.
We have declared another field symbol <fs_line> for the work area. We are looping the
internal table<fs_table> to <fs_line> one by one and printing the individual fields. This
code might look okay at the first time. However this will give us a syntax error as below.
Note that the field symbol <fs_line> is a generic one ( type any ) and it has to be a
generic one since we do not know the line type of the internal table when we write this
method. So, any statement which is trying to access the components of this generic field
symbol will throw an error as above. So, in this we need to access the fields/ components of
the work area (ls_line) using the assign component of structure keyword which we
saw earlier.
Please refer the code below.
Since we do not know how many components are there in the line type of the internal table,
we need a mechanism to know it. When we assign each components of the structure to
another field symbol <fs_field>, if the assignment was successful, the sy-subrc will be
initial. So, when it is not initial, we can assume that there are no more components or
columns.
Input:
Output: