Here’s how a simple ALV list can be programmed. This is one of the most basic tasks you get assigned to as a developer.
Step I – Declaring variables
REPORT ZALVTEST. TABLES: t001w. DATA: gt_plants TYPE TABLE OF t001w. DATA: gr_alv TYPE REF TO cl_salv_table.
The ‚TABLES‚ keyword declares a structure (type: T001W = table of plants) which is refered to when we define our SELECT-OPTIONS.
gt_plants is an internal table (a.k.a. ‚array‘) which holds the data to be displayed in an ALV
gr_alv is a so called object reference to the ALV class (Transaction SE24 => ‚cl_salv_table‘)
Step II – Creating a selection screen
In order for the user to be able to select certain plants, we give him select options.
SELECT-OPTIONS: so_werks FOR t001w-werks, so_land1 FOR t001w-land1.
See how the previously declared structure T001W is referenced here (FOR t001w-werks).
Step III – Reading data with Open SQL
START-OF-SELECTION. SELECT * FROM t001w INTO TABLE gt_plants WHERE werks IN so_werks AND land1 IN so_land1.
The keyword ‚START-OF-SELECTION‚ declares a so called event. Basically it defines the code block where most of the action happens.
See how the select options are used in the WHERE clause of the SQL statement.
Hint: If the select options are left empty on the selection screen, all data are being selected.
Step IV – Creating and displaying the ALV table
END-OF-SELECTION. IF NOT gt_plants IS INITIAL. * Build alv reference CALL METHOD cl_salv_table=>factory IMPORTING r_salv_table = gr_alv CHANGING t_table = gt_plants. * Finally, show alv CALL METHOD gr_alv->display( ). ELSE. MESSAGE i208(00) WITH 'No plants found!'. ENDIF.
The keyword ‚END-OF-SELECTION‚ basically says: All main operations complete, start the final actions.
The first thing we do inside the END-OF-SELECTION code block is to check if the internal table which should be displayed holds any data. If not, we display a basic information.
Now to a little ABAP objects:
In order to display the ALV, we have to do 2 things at least:
a) Create the ALV object
b) Tell the ALV object to display itself.
a) Is done via a static method call of the class CL_SALV_TABLE and method FACTORY.
The internal table GT_PLANTS is handed over to the method and the object reference GR_ALV is given back to us (us = the calling program).
– You can identify static method calls via the ‚=>‘
– Static method means: You don’t have to create an instance of the class prior to calling the method.
b) This is done via a so called instance method call of the object GR_ALV and method DISPLAY.
– You can identify instance method calls via the ‚->‘
– Calling instance methods is only possible if you have an object reference which, in this case, we got from a) 🙂