Totals and Subtotals in ALV factory methods

Display totals and subtotals in ALV with factory methods, aggregations in ALV, sort in ALV

+ -

Most of the times, we may need to display totals and subtotals in ALV reports, in this lesson you will be able to learn how to display totals and subtotals in ALV with factory methods.

Requirement:Display purchase order details for a range of purchase orders, display totals and subtotals for a purchase order number .

Requirement Analysis: For the above requirement, we need to get data from EKPO(purchase order item table), display subtotals for each purchase order number(EBELN field)(one purchase order can have multiple items) and display totals of all purchase orders at the bottom.

Step 1: Data declarations, get data and get factory instance.

REPORT ZSAPN_ALV_FACTORY_TOTALS.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
        EBELN TYPE EKPO-EBELN,
        EBELP TYPE EKPO-EBELP,
        MATNR TYPE EKPO-MATNR,
        BUKRS TYPE EKPO-BUKRS,
        MENGE TYPE EKPO-MENGE,
      END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
       WA_EKPO TYPE TY_EKPO. "work area

START-OF-SELECTION.
  SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE EBELN IN S_EBELN. "get po data
  DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv referance

*TRY.
  CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
*  EXPORTING
*    LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
*    R_CONTAINER    =
*    CONTAINER_NAME =
    IMPORTING
      R_SALV_TABLE = LR_ALV
    CHANGING
      T_TABLE      = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.

Step 2: Add totals

To add totals we need to use GET_AGGREGATIONS, once we get aggregations instance, we need to add aggregation by passing column name and aggregation type to method ADD_AGGREGATION.

  DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.
  LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
*   Add TOTAL for COLUMN NETWR
  TRY.
      CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
        EXPORTING
          COLUMNNAME  = 'MENGE' "aggregation column name
          AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

    CATCH CX_SALV_DATA_ERROR .                          "#EC NO_HANDLER
    CATCH CX_SALV_NOT_FOUND .                           "#EC NO_HANDLER
    CATCH CX_SALV_EXISTING .                            "#EC NO_HANDLER
  ENDTRY.

Step 3: Add subtotals

To add subtotals, we need to add sort to the columns and then we have to use SET_SUBTOTAL method to display subtotals.
  DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
  CALL METHOD LR_ALV->GET_SORTS "get sorts
    RECEIVING
      VALUE = LR_SORT.
*TRY.
  DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
  CALL METHOD LR_SORT->ADD_SORT "add column sort
    EXPORTING
      COLUMNNAME = 'EBELN' "sort column always keyfield
*     POSITION   =
*     SEQUENCE   = IF_SALV_C_SORT=>SORT_UP
*     SUBTOTAL   = IF_SALV_C_BOOL_SAP=>FALSE
*     GROUP      = IF_SALV_C_SORT=>GROUP_NONE
*     OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
    RECEIVING
      VALUE      = LR_SORT_COLUMN.

*  TRY.
  CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
    EXPORTING
      VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
*   CATCH CX_SALV_DATA_ERROR .
*  ENDTRY.

Step 4: Display ALV

  LR_ALV->DISPLAY( ). "display ALV

Final code will be

REPORT ZSAPN_ALV_FACTORY_TOTALS.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
        EBELN TYPE EKPO-EBELN,
        EBELP TYPE EKPO-EBELP,
        MATNR TYPE EKPO-MATNR,
        BUKRS TYPE EKPO-BUKRS,
        MENGE TYPE EKPO-MENGE,
      END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
       WA_EKPO TYPE TY_EKPO. "work area

START-OF-SELECTION.
  SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE EBELN IN S_EBELN. "get po data
  DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv referance
*TRY.
  CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
*  EXPORTING
*    LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
*    R_CONTAINER    =
*    CONTAINER_NAME =
    IMPORTING
      R_SALV_TABLE = LR_ALV
    CHANGING
      T_TABLE      = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.

  DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.
  LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
*   Add TOTAL for COLUMN NETWR
  TRY.
      CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
        EXPORTING
          COLUMNNAME  = 'MENGE' "aggregation column name
          AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

    CATCH CX_SALV_DATA_ERROR .                          "#EC NO_HANDLER
    CATCH CX_SALV_NOT_FOUND .                           "#EC NO_HANDLER
    CATCH CX_SALV_EXISTING .                            "#EC NO_HANDLER
  ENDTRY.
*   Bring the total line to top
  DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
  CALL METHOD LR_ALV->GET_SORTS "get sorts
    RECEIVING
      VALUE = LR_SORT.
*TRY.
  DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
  CALL METHOD LR_SORT->ADD_SORT "add column sort
    EXPORTING
      COLUMNNAME = 'EBELN' "sort column always keyfield
*     POSITION   =
*     SEQUENCE   = IF_SALV_C_SORT=>SORT_UP
*     SUBTOTAL   = IF_SALV_C_BOOL_SAP=>FALSE
*     GROUP      = IF_SALV_C_SORT=>GROUP_NONE
*     OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
    RECEIVING
      VALUE      = LR_SORT_COLUMN.
*  TRY.
  CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
    EXPORTING
      VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
*   CATCH CX_SALV_DATA_ERROR .
*  ENDTRY.
* CATCH CX_SALV_NOT_FOUND .
* CATCH CX_SALV_EXISTING .
* CATCH CX_SALV_DATA_ERROR .
*ENDTRY.
  LR_ALV->DISPLAY( ). "display ALV

Output

ALV totals and subtotals factory method

Please Sign in to ask a question

Was this lesson helpful to you? Yes No 23 People out of 24 think this lesson helpful


Lesson Navigation

←Previous Chapter
Next Chapter →