Classical ALV: Change Subtotal – II for Print

By | March 8, 2009 | Classical ALV | 12,079 | 9

Change in the ALV Subtotal for the display & print

In the post, Classical ALV: Change Subtotal, we have seen how we can change the Subtotal on the Classical ALV. Today, we will see how we can set the subtotal in the ALV display and the print function – Print Preview & Print.

We will use the event TOP_OF_LIST to perform this 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 Sub total in the print. For this purpose we will use the field-symbol to get an access the table (SAPLKKBL)IT_COLLECT01[]

Code Snippet to change the Subtotal for Display & Print

REPORT  zalv_subtot_change.
*
*&---------------------------------------------------------------------*
*& Report  ZALV_SUBTOT_CHANGE.
*&
*&---------------------------------------------------------------------*
*& Program shows how to change the Subtotal of the classical ALV
*& check subroutine TOP_OF_PAGE
*&
*&---------------------------------------------------------------------*
*
TYPE-POOLSslis.
*
TYPESBEGIN OF ty_bkpf,
       belnr TYPE bkpf-belnr,
       buzei TYPE bseg-buzei,
       dmbtr TYPE bseg-dmbtr,
       per   TYPE bseg-dmbtr,
       per1  TYPE char10,
       mode  TYPE bapi_change_mode,
       END   OF ty_bkpf.
*
DATAit_bkpf TYPE STANDARD TABLE OF ty_bkpf,
      it_bkpf1 TYPE STANDARD TABLE OF ty_bkpf,
      wa_bkpf TYPE ty_bkpf.
*
DATAt_fieldcat   TYPE slis_t_fieldcat_alv.
DATA:  wa_fieldcat   TYPE slis_fieldcat_alv.
*
START-OF-SELECTION.
*
  SELECT belnr buzei dmbtr
         INTO TABLE it_bkpf
         FROM  bseg
         UP TO 20 ROWS.
*
  LOOP AT it_bkpf INTO wa_bkpf.
    wa_bkpf-per 0.
    wa_bkpf-mode 'O'.
    MODIFY it_bkpf FROM wa_bkpf.
    CLEAR  wa_bkpf.
  ENDLOOP.
*
  PERFORM create_field_catalog.
  PERFORM create_alv_output.
*
*&---------------------------------------------------------------------*
*&      Form  create_field_catalog
*&---------------------------------------------------------------------*
FORM create_field_catalog .
*
  PERFORM add_field_catalog USING :
    '01' 'BELNR'     'Doc'         '10'  '',
    '02' 'BUZEI'     'Line'        '3'  '',
    '03' 'DMBTR'     'Amount'      '17'  'X',
    '04' 'PER'       'Percentage'      '17'  'X',
    '05' 'PER1'      'Percentage'     '17'  '',
    '06' 'MODE'      'Mode'        '4'   ''.
*
ENDFORM.                    " create_field_catalog
*
*&---------------------------------------------------------------------*
*&      Form  add_field_catalog
*&---------------------------------------------------------------------*
*       Adds field details into field catalog
*----------------------------------------------------------------------*
FORM add_field_catalog  USING    p_colpos
                                 p_fldname
                                 p_fldtext
                                 p_outlen
                                 p_sum..
*
  wa_fieldcat-row_pos       '1'.
  wa_fieldcat-col_pos       p_colpos.
  wa_fieldcat-fieldname     p_fldname.
  wa_fieldcat-tabname       'IT_BKPF'.
  wa_fieldcat-reptext_ddic  p_fldtext.
  wa_fieldcat-outputlen     p_outlen.
  wa_fieldcat-no_zero       'X'.  " <<
  wa_fieldcat-do_sum        p_sum.
  IF wa_fieldcat-fieldname 'MODE'.
    wa_fieldcat-rollname      'BAPI_CHANGE_MODE'.
  ENDIF.
  APPEND  wa_fieldcat TO t_fieldcat.
  CLEAR wa_fieldcat.
*
ENDFORM.                    " add_field_catalog
*&---------------------------------------------------------------------*
*&      Form  create_alv_output
*&---------------------------------------------------------------------*
*       Generate ALV Grid output
*----------------------------------------------------------------------*
FORM create_alv_output .
  DATAl_repid LIKE sy-repid,
        l_layout TYPE slis_layout_alv,
        l_print  TYPE  slis_print_alv.
*
  DATAit_sort TYPE slis_t_sortinfo_alv,
        ls_sort TYPE slis_sortinfo_alv.
*
  DATAit_filter TYPE slis_t_filter_alv,
        ls_filter TYPE slis_filter_alv.
*
  DATAit_event_exit TYPE slis_t_event_exit,
        ls_event_exit TYPE slis_event_exit.
*
  DATAt_event      TYPE slis_t_event,
        wa_event      TYPE slis_alv_event.
*
*
  l_repid sy-repid.
*
  l_layout-no_totalline 'X'.
  l_layout-colwidth_optimize ' '.
*
  ls_sort-spos '1'.
  ls_sort-fieldname 'BUZEI'.
  ls_sort-tabname 'IT_BKPF'.
  ls_sort-up 'X'.
  ls_sort-subtot 'X'.
  APPEND ls_sort TO it_sort.
*
*
  ls_event_exit-ucomm '&ILT'.
  ls_event_exit-after 'X'.
  APPEND ls_event_exit TO it_event_exit.
*
*
  CLEAR wa_event.
  wa_event-name 'USER_COMMAND'.
  wa_event-form 'USER_COMMAND'.
  APPEND wa_event TO t_event.
*
  CLEAR wa_event.
  wa_event-name 'TOP_OF_PAGE'.
  wa_event-form 'TOP_OF_PAGE'.
  APPEND wa_event TO t_event.
*
  CLEAR wa_event.
  wa_event-name 'TOP_OF_LIST'.
  wa_event-form 'TOP_OF_LIST'.
  APPEND wa_event TO t_event.
*
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program     l_repid
      i_callback_top_of_page 'TOP_OF_PAGE'
      is_layout              l_layout
      is_print               l_print
      it_sort                it_sort
      it_filter              it_filter
      it_fieldcat            t_fieldcat[]
      it_events              t_event
    TABLES
      t_outtab               it_bkpf
    EXCEPTIONS
      program_error          1
      OTHERS                 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
*
ENDFORM.                    " create_alv_output
*
*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&---------------------------------------------------------------------*
FORM top_of_page.
*
  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'.
*
ENDFORM.                    " top_of_page
*
*
*&---------------------------------------------------------------------*
*&      Form  TOP_OF_LIST
*&---------------------------------------------------------------------*
FORM top_of_list.
*
  DATAlo_grid TYPE REF TO cl_gui_alv_grid.
*
* FOR Print out:
  CHECK sy-ucomm 'PRIN'            " Print
  OR    sy-ucomm '&RNT_PREV'.      " Print Preview
*
* 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.
*
* Set the subtotal for the Print / Print preview
  FIELD-SYMBOLS<fs_tab1> TYPE ANY TABLE.
  DATAl_tab_name TYPE string.
*
  l_tab_name '(SAPLKKBL)IT_COLLECT01[]'.
*
  ASSIGN (l_tab_nameTO <fs_tab1>.
  IF <fs_tab1> IS ASSIGNED.
*
    <fs_tab1> <ft_tab>.
*
  ENDIF.
*
ENDFORM.                    " top_of_list

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

9 Comments

  • Alejandro G.

    Hello!
    Thanks very much for the information.
    I used it using Object Oriented Alv Grid, it’s all the same, you just have to put the code in the method which handles the event “print_top_of_list”.
    Best regards from Argentina.

  • hi naimesh, your blog posts are great. Keep writing.

    I had tried this code in SAP 4.7 and ECC 6. It works well in ECC 6, but in 4.7 the subtotal doesn't show up in print preview. Have you tried this code in 4.7 ?

    Thank you for your reply.

  • Anonymous

    Hi I have a problem exporting this list to excel.
    If the user exports from the GRID view trh subtotals appear wrong. But if they export from the Print preview then everything is ok.
    Any comments will be appriciated.
    Thanks

  • Anonymous

    I have the exact same problem. Does anyone know how to fix this?

    Thanks

  • Anonymous

    Hi, although the questions above are quite old… maybe somone comes across the same problem -> the solution is to add function code %PC =>
    * FOR Print out:
    CHECK sy-ucomm = 'PRIN' " Print
    OR sy-ucomm = '&RNT;_PREV' " Print Preview
    OR SY-UCOMM ='%PC'. "Download to Excel

  • Anonymous

    hi,
    this post helps me a lot.
    thanks for posting a useful posts

    thanks a lot
    regards
    karthe

  • Anonymous

    Hi all,

    I have an issue with ALV grid export to excel 2003 ,when i tried to download with the option as local file > xls file ,the report downloads with page breaks which the user does not want , each break comeswith header .

    With the option of list>export>xml sheet – the report downloads with out page break but with no header information like the name of the report and reporting period .The tcode is f.01 and the system is ECC6.

    Can any one suggest how to download the report with tiltle of the report with no page break ?

  • Anonymous

    Hello Naimesh,
    Thank you very much – good work !

    Is there a way to make to work also with jobs (in the background mode) ?

    Hugo

  • Elly L.

    Hello Naimesh,

    Thanks for yr help.
    I implemented yr sample code and saw that the functionality of the Context menu (Right click) “Copy text”, “Hide” “Freezing” etc” has actually been disabled.

    Is there a solution to that.

    Kind Regards,
    Elly L.

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.