No products in the cart.
ABAP
The OOPS ABAP ALV Code
In modern ABAP, the procedural function module approach is often replaced by the ALV Object Model (SALV). This approach is more robust, uses classes/methods, and requires significantly less code because the system handles the metadata (column headers) automatically.
The primary class used for this is CL_SALV_TABLE.
The OOPS ABAP ALV Code
REPORT z_simple_alv_oops.
* 1. Data Declaration
DATA: it_sflight TYPE TABLE OF sflight,
lo_alv TYPE REF TO cl_salv_table, ” Reference to the ALV Object
lo_msg TYPE REF TO cx_salv_msg. ” Reference for Exception Handling
* 2. Data Retrieval
START-OF-SELECTION.
SELECT * FROM sflight INTO TABLE it_sflight UP TO 100 ROWS.
* 3. Display ALV using Object Oriented Method
IF it_sflight IS NOT INITIAL.
TRY.
” Factory method creates the instance of the ALV object
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = it_sflight ).
” Display the grid
lo_alv->display( ).
CATCH cx_salv_msg INTO lo_msg.
” Handle errors, like display issues
MESSAGE lo_msg->get_text( ) TYPE ‘E’.
ENDTRY.
ELSE.
MESSAGE ‘No data found.’ TYPE ‘I’.
ENDIF.
Understanding the OO Logic
Unlike the procedural method where you pass parameters into a function, the OO approach treats the report as an object instance.
| Component | Description |
| cl_salv_table=>factory | A static method that “manufactures” the ALV object. It automatically maps the fields of your internal table to the grid. |
| lo_alv | This is the object reference (pointer). Once the factory creates it, lo_alv holds all the properties and methods of your report. |
| TRY…CATCH | OOPS uses Exception Classes for error handling. If something goes wrong during object creation, the CATCH block prevents the program from crashing (dumping). |
| lo_alv->display( ) | This is a method call. It tells the object to finally render the data on the user’s screen. |
Why use OOPS for ALV?
Type Safety: The compiler catches more errors at design time compared to function modules.
Clean Syntax: You don’t have to manage long lists of “Exporting/Importing” parameters.
Extensibility: If you want to add a toolbar or double-click functionality, you simply fetch a “sub-object” (like lo_functions = lo_alv->get_functions( )) and trigger a method.