Copyright 2023 - BV TallVision IT

Can an ALV report be refreshed automatically every so-many seconds ? Yes it can and this article explains how. If you have an ALV report which already has refresh functionality, simply follow the step-plan to auto refresh it. 

Automatic refresh involves a timer and responding to the timer. There is a standard SAP class CL_GUI_TIMER available for this. Now if you look up examples for automatic refresh on ALV reports, you will find quite a few of them based on the REUSE_ALV_GRID_DISPLAY_LVC function module or class CL_GUI_ALV_GRID, which is NOT my preferred class This example is applied to the CL_SALV_TABLE class: 

So you already have an ALV wih a refresh "button" in place. If not, refer to this article ALV refresh before continuing here. The steps that are explained below will involve a timer in your coding, with an interval defined in seconds. So the automatic refresh will be done every (e.g.) 30 seconds. A single timer operation run will consume about a second, so there is no need to ever set an interval for less than a second. The steps:

  1. Define an event handler for the timer, the lcl_timer_handler. This will respond to an event that the timer class will throw, so this is where - for this example - the refresh actions will occur.
    CLASS lcl_timer_handler DEFINITION.
        CLASS-METHODS: on_finished FOR EVENT finished OF cl_gui_timer IMPORTING sender.
  2. In your main "controller" class, (e.g. lcl_controller) you will need to define 2 TYPE REF TO variables, like so:
    data: go_timer TYPE REF TO cl_gui_timer,
          go_timer_handler TYPE REF TO lcl_timer_handler.
    The go_TIMER will hold the actual CL_GUI_TIMER instance and the go_TIMER_handler will hold the instance of the event handling class we have defined in the previous step.
  3. Now the timer needs to be started, As automatic refresh is something quite unusual, it's probably a good idea to make it optional, so the selection screen of you report should have .e.g. a checkbox option to enable/disable automatic refresh. Here's a method that should be added to your controller class (in this example class lcl_controller):
      METHOD start_timer.
    * Create an instance of the Timer
        CREATE OBJECT go_timer.
        CREATE OBJECT go_timer_handler.
    * Set an event handler to capture the timer-signal
        SET HANDLER go_timer_handler->on_finished FOR go_timer.
    * Set the interval in seconds
        go_timer->interval = 29.
    * Start the times
        go_timer->run(  ).
    The START_TIMER method has no parameters and is defined as a CLASS-METHODS of the PUBLIC-SECTION of your controller class. What this wil do is (1) create the necessary object instances, (2) set the handler for the FINISHED event of the timer class cl_GUI_TIMER, (3) set the interval to 29 seconds and (4) start the timer.
  4. After the 29 seconds have passed, the event FINISHED will be thrown which will execute method on_FINISHED of your very own class lcl_TIMER_handler. This is where the refresh actions need to take place. The complete implementation of the lcl_TIMER_handler class:
    CLASS lcl_timer_handler IMPLEMENTATION.
      METHOD: on_finished.
        cl_gui_cfw=>set_new_ok_code( new_code = 'REFRESH' ).
        sender->run( ).

    So why set an okcode ? Why not simply execute the report selection again and refresh the ALV with the classic lcl_controller=>go_salv->refresh( ) ? Well, just try it and you see that it won't work. Setting the OK code will.

  5. There is one last thing you need to consider: what if the actual refresh (which involved re-selecting data) is slower than the timer interval time. So what if the selection takes 40 seconds, and the timer triggers a refresh every 30 seconds. Will the system die ? Not necessarily: the interval is re-applied after the refresh is finished. So the first refresh would be at second 30, the second one seconds 70 (30+40) later. That's what the sender->run() is for.

You can find a working example report that uses this technology to auto-refresh in article Big Brother.