Creating Basic ALV Grid Report in SAP ABAP

ALV (ABAP List Viewer) reports play a crucial role in the world of SAP ABAP development. They provide a powerful tool for presenting data in a user-friendly and organized manner. In this blog, we will walk you through the process of creating a basic ALV Grid report in SAP ABAP, step by step.



Step 1: Define the Data Source

The first step in creating an ALV Grid report is to define the data source. This typically involves fetching data from database tables or other sources. For this example, let's consider a scenario where we want to display a list of employees and their basic information.


Step 2: Create the Internal Table

Once you've fetched the data, you need to store it in an internal table. An internal table is a dynamic structure that holds data in memory. Define the structure of the internal table based on the fields you want to display in the ALV report.


DATA: lt_employee TYPE TABLE OF employee_data, " Replace with actual data structure
      ls_employee TYPE employee_data. " Replace with actual data structure
" Fill the internal table with data
SELECT * FROM employee INTO TABLE lt_employee.
```

Step 3: Create ALV Grid Object

Next, create an instance of the ALV Grid object using the `CL_GUI_ALV_GRID` class.


DATA: lo_alv_grid TYPE REF TO cl_gui_alv_grid.
" Create ALV Grid object
CREATE OBJECT lo_alv_grid.

```


Step 4: Set Field Catalog

The field catalog defines the structure of the ALV report, including column headers and their properties. Define the field catalog based on the structure of your internal table.



DATA: lt_fieldcat TYPE TABLE OF lvc_s_fcat,
      ls_fieldcat TYPE lvc_s_fcat.
" Define field catalog
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = 'EMPLOYEE_ID'.
ls_fieldcat-seltext_m = 'Employee ID'.
APPEND ls_fieldcat TO lt_fieldcat.
" Add more columns to the field catalog
" ls_fieldcat-fieldname = 'EMPLOYEE_NAME'.
" ls_fieldcat-seltext_m = 'Employee Name'.
" APPEND ls_fieldcat TO lt_fieldcat.
" Continue adding columns to the field catalog as needed

```



Step5: Set Layout and Display ALV Grid

After defining the field catalog, you need to set the layout of the ALV Grid and display it.


"

Set layout

lo_alv_grid->set_table_for_first_display(

  EXPORTING

    i_structure_name = 'EMPLOYEE_DATA'

  CHANGING

    it_outtab = lt_employee

    it_fieldcatalog = lt_fieldcat

).

```


Step 6: Handle User Interactions

ALV Grid reports provide user interaction features such as sorting, filtering, and column resizing. However, these features are enabled by default. If you wish to enable additional user interactions, you can do so using event handling.


"
Enable sorting
lo_alv_grid->set_sort_allowed( abap_true ).
" Enable filtering
lo_alv_grid->set_filter( abap_true ).
" Register double-click event
SET HANDLER double_click_event FOR lo_alv_grid.

``

 Step 7: Define Event Handler Methods

Define event handler methods to handle user interactions like double-clicking on a row.


METHOD double_click_event.
  DATA: lv_row TYPE i,
        lv_employee_id TYPE employee_data-employee_id. " Replace with actual data field
  lv_row = iv_event->get_row( ).
  READ TABLE lt_employee INDEX lv_row INTO ls_employee.
  lv_employee_id = ls_employee-employee_id.
  " Perform action on double-click event
  " For example, display employee details or navigate to employee's profile.
ENDMETHOD.

```



Step 8: Finalize and Test

With all the steps in place, your basic ALV Grid report is ready for testing. Run your ABAP program, and you should see the ALV report displaying the employee data in a tabular format. Users can interact with the report by sorting, filtering, and performing other actions based on your configurations.


In conclusion, creating a basic ALV Grid report in SAP ABAP involves fetching data, defining an internal table, creating an ALV Grid object, setting up the field catalog, and displaying the report. With additional event handling, you can enhance user interaction and provide a more dynamic experience. ALV reports offer a powerful way to present data, making them an essential tool for SAP developers in various projects.

Previous
Next Post »