Sometimes we need to modify the subtotals on the ALV, specially when we have to give the average of the percentages or something like that. To change the subtotal we need to follow certain steps:
1. we need to get the ALV object form the ALV function module. We can use the FM GET_GLOBALS_FROM_SLVC_FULLSCR to get the Global data of the ALV. From this FM we will get the ALV object.
2. After getting the ALV object, we need to get the subtotal using the method GET_SUBTOTALS of the ALV object. We will get the first level subtotal using the parameter EP_COLLECT01.
3. Now, we need to modify the subtotal. Here we need to take help of Field-symbols since the EP_COLLECT01 is reference to data.
4. We need to refresh the Table display. For this purpose we can use the method REFRESH_TABLE_DISPLAY.
Here is the code snippet which performs all the steps mentioned above:
DATA: lo_grid TYPE REF TO cl_gui_alv_grid.
* get the global reference
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
e_grid = lo_grid.
* get the subtotal
DATA: it_01 TYPE REF TO data.
CALL METHOD lo_grid->get_subtotals
ep_collect01 = it_01.
* change the data
FIELD-SYMBOLS: <ft_tab> TYPE ANY TABLE,
<fs_tab> TYPE ANY,
<ff_field> TYPE ANY.
ASSIGN it_01->* TO <ft_tab>.
LOOP AT <ft_tab> ASSIGNING <fs_tab>.
ASSIGN COMPONENT 'PER' OF STRUCTURE <fs_tab> TO <ff_field>.
<ff_field> = '100'.
* Refresh the table display
CALL METHOD lo_grid->refresh_table_display
i_soft_refresh = 'X'.
To be able to use the FM GET_GLOBALS_FROM_SLVC_FULLSCR, we need to find a spot in our program which is being called after the ALV’s main FM e.g. REUSE_ALV_GRID_DISPLAY. For this purpose, I have implemented the TOP_OF_PAGE event because it will always be called after the FM. In the subroutine for the TOP_OF_PAGE, I used the code snippet to change the Subtotal.
You can download the entire source code of my test program from here.
This forum question leads me to write this blogpost: Can we modify a sub-total in ALV?
To Retain the calculated Subtotal in the Print Preview and Print, visit Classical ALV: Change Subtotal II for Print.