How To Auto Refresh ALV Grid Display Using CL_GUI_TIMER?

One common challenge in SAP reporting using ALV is the need to keep data displayed on an ALV grid up to date without requiring manual user intervention. For example, displaying queue status, monitoring live data movement such as stock levels, order statuses, or real time transactions, it is crucial that the program will display information reflects the latest changes dynamically.

So without an auto-refresh mechanism, users must repeatedly close and reopen the report or manually trigger a refresh to view updated data, which is both inefficient and inconvenient.

In this ABAP tutorial, we will learn how to implement an auto refresh functionality in your ALV display. With this feature, the ALV will automatically reflect any changes whenever a record in the table is updated.

Tutorial Objectives
1. Learn how to implement ALV auto refresh by using CL_GUI_TIMER
2. Learn how to set interval in CL_GUI_TIMER to display the latest changes on ALV.
3. You can display records changes (auto refresh) from the table into ALV output automatically.
Prerequisites
1. SAP System Access: SAP GUI, ECC
2. Authorization: Developer role, SE38

The ALV Auto Refresh Code Example

In below source code, we are using CL_GUI_TIMER to display changes automatically at regular intervals. This ensures that the information displayed in your ALV output remains current and accurate, improving efficiency and user experience.

The ALV Grid class that we’re using in this sample is CL_GUI_ALV_GRID.

report z_auto_refresh.
*&---------------------------------------------------------------------*
*& Lesson : Auto refresh ALV output
*& Level  : ABAP Code Example
*& Tutor  : ABAPGeeks.com
*&---------------------------------------------------------------------*
class cl_alv definition.
  public section.
    data : ob_timer  type ref to cl_gui_timer . "An object of type CL_GUI_TIMER,
                                                "sesponsible for triggering the auto-refresh
                                                "functionality at regular intervals.

    methods:
      get_data, "Retrieves data from the SCARR table into the it_data internal table.
      set_field_cat, "Builds the field catalog (gt_fcat) to define the structure of the ALV grid display.
      display_report, "Initializes the ALV grid and sets it up with data and the field catalog.
      timer_event for event finished of cl_gui_timer. "This is the event handler triggered when the timer interval elapses.
  private section.
    data: grid type ref to cl_gui_alv_grid.
    data: it_data type table of scarr.
    data: ls_layo type lvc_s_layo,
          gt_fcat type lvc_t_fcat,
          gs_fcat type lvc_s_fcat.

endclass.

class cl_alv implementation.
  method timer_event.

    get_data( ). "Fetches the latest data by calling get_data.

    if grid is initial.
      call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        importing
          e_grid = grid.
    endif.

    "Refreshes the ALV grid to display updated data using
    "check_changed_data and refresh_table_display.
    call method: grid->check_changed_data,
                 grid->refresh_table_display.

    "Restarts the timer (ob_timer->run) to continue the auto-refresh cycle.
    ob_timer->run( ).

  endmethod.
  method set_field_cat.
"Simplifies the addition of field catalog entries by specifying column position,
"field name, description, and output length.
    define add_fcat.
      clear gs_fcat.
      gs_fcat-col_pos = &1.
      gs_fcat-fieldname = &2.
      gs_fcat-coltext = &3.
      gs_fcat-outputlen = &4.
      append gs_fcat to gt_fcat.
    end-of-definition.

    add_fcat:
       1 'CARRID' 'Carr ID' 5,
       2 'CARRNAME' 'Carrier Name' 20,
       3 'CURRCODE' 'Curr Code.' 5,
       4 'URL' 'Url.' 30.
  endmethod.

  method get_data.
    select * into table it_data from scarr.
  endmethod.
  method display_report.

    "Using this statement, we don't need to create a custom container
    "If you open screen 100, it's just a blank dialog screen without any control
    grid =
      new cl_gui_alv_grid(
          i_parent = cl_gui_container=>default_screen ).

    set_field_cat( ).

    ls_layo-zebra = 'X'.

    call method grid->set_table_for_first_display(
      exporting
        is_layout                     = ls_layo                " Layout
      changing
        it_outtab                     = it_data               " Output Table
        it_fieldcatalog               = gt_fcat               " Field Catalog
      exceptions
        invalid_parameter_combination = 1                " Wrong Parameter
        program_error                 = 2                " Program Errors
        too_many_lines                = 3                " Too many Rows in Ready for Input Grid
        others                        = 4
                                        ).
    if sy-subrc <> 0.
      message id sy-msgid type sy-msgty number sy-msgno
        with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    endif.

    call screen 100. "This is just a blank screen

  endmethod.
endclass.

start-of-selection.
  data: o_alv type ref to cl_alv.
  create object: o_alv, o_alv->ob_timer.
  "Associates the timer_event method with the finished event of the timer object (ob_timer)
  set handler o_alv->timer_event for o_alv->ob_timer.
  o_alv->ob_timer->interval = 3.
  o_alv->ob_timer->run( ).
  o_alv->get_data( ).
  o_alv->display_report( ).
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module status_0100 output.
  set pf-status 'PF-STATUS'.
*  SET TITLEBAR 'xxx'.
endmodule.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module user_command_0100 input.
  IF sy-ucomm = 'BACK' or sy-ucomm = 'EXIT' or sy-ucomm = 'CANCEL'.
    SET SCREEN 0.
  ENDIF.
endmodule.

The ALV Auto Refresh Program Flow

1. Create Objects

The o_alv object of the CL_GUI_ALV_GRID class is created.
The ob_timer object is initialized for managing auto-refresh functionality.

2. Set Timer Properties

The interval for the timer is set to 3 seconds (ob_timer->interval = 3), ensuring data is refreshed every 3 seconds.

3. Trigger the Timer

The run method of the timer is called to start the auto refresh cycle.

4. Fetch Data and Display Report

Initial data is fetched using get_data, and the ALV grid is displayed using display_report.

5. Auto Refresh Cycle

The timer triggers timer_event (interval) for every 3 seconds, updating the ALV grid with the latest data (repeating step 4).

Test The Auto Refresh ALV Output

Step 1. Start by executing the program and letting it run (don’t close it). The ALV grid will display data from the SCARR table, and the auto refresh mechanism will ensure the data updates at regular intervals which 3 seconds.

Next we want to change one of the records directly from the SCARR table.

Step 2. Update one of the records in the SCARR table. In this tutorial I will change the record with CARRID: AF, modify airline name from Air France into Air France Airways.

Step 3. Go back to the ALV program and you will see the changes has been updated automatically.

SAP reference on CL_GUI_ALV_GRID.

Implementation Tips

Since the auto refresh functionality queries the entire records to update your ALV output, it can lead to performance issues when working big size data, the most common issue would be slowness and could impacting other part of your SAP system, just imagine querying >100K records every 3 seconds.

That is why you need a way to refresh only the delta (delta updates) instead of reloading the entire table by creating a staging table for these delta records.

Note: CL_GUI_TIMER cannot run on background because it’s a GUI object.

Share this tutorial with Fellow Geeks!

Other ABAP Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *