Classical ALV: Change Subtotal

By | September 18, 2008 | Classical ALV | 23,958 | 14

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:

DATAlo_grid TYPE REF TO cl_gui_alv_grid.
*
* get the global reference
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid lo_grid.
*
* get the subtotal
  DATAit_01 TYPE REF TO data.
*
  CALL METHOD lo_grid->get_subtotals
    IMPORTING
      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'.
  ENDLOOP.
*
* Refresh the table display
  CALL METHOD lo_grid->refresh_table_display
    EXPORTING
      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?

Update:
To Retain the calculated Subtotal in the Print Preview and Print, visit Classical ALV: Change Subtotal II for Print.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

14 Comments

  • Anonymous

    That was a good one Naimesh
    Really appreciate your knowledge.

    Regards,
    DNP

  • Wing

    Dear Naimesh, it only changes the value when display, when export or print, the values will be the origin one.
    please advice.

  • Anonymous

    Congratulations .

  • Anonymous

    Hi Naimesh,
    I also am having problems to update totals when exporting and printing. any ideas?
    Thanks
    Sanje

  • Hello Wing and Sanje,

    This is currently a known issue. I am researching on how to get them in export and print.

    Regards,
    Naimesh Patel

  • Anonymous

    Hello Naimesh,

    have you already found a solution, how to export or print ALV with changed subtotals?

    Thanks,
    Noémi

  • Hello ALL,

    Solution to retain the changed Subtotal in the Print can be found in the post: http://help-abap.blogspot.com/2009/03/classical-alv-change-subtotal-ii-for.html

    Regards,
    Naimesh Patel

  • Anonymous

    Hello,

    can i change the sub-total line in SALV class also? Or is that in ne new salv class not available?

  • Hello Naimesh,
    When i refresh the screen, the subtotals expand/collapse situation does not return to the last one. The get_subtotals method- ET_GROUPLEVELS table gets the relevant data, but it is not ref to data and there is no set_subtotals method. How can i pass the subtotals expand/collapse situation to the screen? I've been searching for a solution already for few days…

  • Hello Naimesh

    I am new to this site. congratus to you.. Have a nice day

    just i need small favour we developed one Programme for Daily Progress Report ZPS_DPR

    it is working with Exisiting Plant. but not working with New Plant which we configured. showing only plant details nor showing activities wbs element etc.

    where we need to look up

    ours abapers isnot available this time

    hope you will find the way.

    Thanks

    Rajesh G

  • Hello Rajesh,

    Check if it has hardcoded values for the plant for which it is giving correct result. Also check if there is any authorization which prevent this display.

    Regards,
    Naimesh Patel

  • Hello Diana & Moshe,

    I have never tried with the hierarchical ALV to change the Subtotal. I give a try and post the result here.

    Regards,
    Naimesh Patel

  • Hi Naimesh,

    I am working on a tree reprot using class CL_GUI_ALV_TREE. Here I have a requiment to calculate percentages at every line and node level. When I am calculating percentages at node level I need to use the values in the row.
    I am getting the sum of the other columns by with the combination of DO_SUM = X and update_calculations method.

    Is there any way to calculate the percentages at every node level.

    Thanks,
    Ramesh

  • Gustavo

    Naimesh,

    Tank You Very Much for your solution about the topic “Classical ALV: Change Subtotal” ….

    it’s helped me a lot!!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.