sapdev logo background
sapdev logo sapdev logo

ALV totals text

In order to create a custom totals text you would think that all you need to do is update the layout field totals_text with the specified text (i.e. gd_layout-totals_text = 'Totals'.). Unfortunately this does not seem to work so another solution is required. One method is to add a specific text field to your output table and use this to display custom totals text via a sort catalog declaration and setting the correct fieldcatalog values. Here are the basic steps involved:

Step 1. First create a Basic ALV report.

Step 2. Add field to ALV output table

  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  statu TYPE ekpo-statu,
  aedat TYPE ekpo-aedat,
  matnr TYPE ekpo-matnr,
  menge TYPE ekpo-menge,
  meins TYPE ekpo-meins,
  netpr TYPE ekpo-netpr,
  peinh TYPE ekpo-peinh,
  TEXT(50) TYPE c,

Step 3. Add data declaration for sort catalogue

*  ALV data declarations
  data: it_sortcat   type slis_sortinfo_alv occurs 1,
        wa_sort like line of it_sortcat.

Step 4. Add new field to field catalogue using below values

  fieldcatalog-fieldname   = 'TEXT'.
  fieldcatalog-seltext_m   = 'Totals'.
  fieldcatalog-tech        = 'X'.
  fieldcatalog-no_out      = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

Step 5. Total ALV by specific field
For this example i am going to total the NETPR field so am adding the fieldcatalog-do_sum = 'X' declaration to this field when building the fieldcatalog.

  fieldcatalog-fieldname   = 'NETPR'.
  fieldcatalog-seltext_m   = 'Net Price'.
  fieldcatalog-col_pos     = 7.
  fieldcatalog-outputlen   = 15.
  fieldcatalog-do_sum      = 'X'.
  fieldcatalog-datatype     = 'CURR'.

Step 5. Add new layout declaration to remove standard totals line

  gd_layout-no_input          = 'X'.
  gd_layout-colwidth_optimize = 'X'.
  gd_layout-totals_text       = 'Totals'(201).
* Do not display standard totals line
  gd_layout-no_totalline = 'X'.

Step 6. Add code to build sort catalogue

 perform build_sortcat.

*&      Form  build_sortcat
*       Build Sort catalog
FORM build_sortcat .
  wa_sort-spos      = 1.
  wa_sort-fieldname = 'TEXT'.
  wa_sort-SUBTOT    = 'X'. "subtotals any totals column by this field
  wa_sort-up        = 'X'.
  APPEND wa_sort TO it_sortcat.
ENDFORM.                    " build_sortcat

Step 7 Update 'REUSE_ALV_GRID_DISPLAY' FM call to include parameter 'it_sort'

 call function 'REUSE_ALV_GRID_DISPLAY'
            i_callback_program      = gd_repid
            i_callback_top_of_page   = 'TOP-OF-PAGE'
            is_layout               = gd_layout
            it_fieldcat             = fieldcatalog[]
            it_sort                 = it_sortcat
            i_save                  = 'X'
            t_outtab                = it_ekko
            program_error           = 1
            others                  = 2.

Step 8 Add aditional sub total text
Once you have your program working upto step 7 in order to add additional totals text and split your report up into subtotals you can populate this extra text field with any text you like.

*&      Form  DATA_RETRIEVAL
*       Retrieve data form EKPO table and populate itab it_ekko
form data_retrieval.
data: ld_subtot(1) type c.

select ebeln ebelp statu aedat matnr menge meins netpr peinh
 up to 10 rows
  from ekpo
  into table it_ekko.

*Populate field with text value
loop at it_ekko into wa_ekko.
* Populate color variable with colour properties
  ld_subtot = ld_subtot + 1.
  wa_ekko-text = ld_subtot.
  modify it_ekko from wa_ekko.

endform.                    " DATA_RETRIEVAL

Although this solution does the job it is not idea as once the user starts messing around with the sort order and subtotal values within the report this information seems to disappear. Is a good starter for 10 though and hopefully with a bit of feedback this method can be improved.

comments powered by Disqus