Sunday, August 20, 2017

Use of ALV To Calculate TOTAL and SORT Table for Sub Total Calculation

The ALV Field Catalog is a critical feature in SAP ABAP that enables users to define and personalize the fields and columns in their ALV reports. One of the useful functions that can be included in the field catalog is the capability to calculate subtotals and totals for specific columns or groups of columns.

To add this function, users need to create the field catalog in the ABAP Dictionary. This involves defining a structure with the necessary fields and screen elements to present the ALV report. Within this structure, the user can determine which columns will be included in the report and how they will be calculated.

After creating the field catalog, users can set up the table for subtotal calculation. This requires defining a structure to store the subtotals and totals and specifying any grouping criteria for the subtotals.

To compute the subtotals and totals, the user must then define the applicable fields in the field catalog as SUM fields, which will conduct the necessary calculations. The user can also configure the sorting logic for the subtotals, which will determine how the subtotals are grouped and displayed in the report.

Overall, the ALV Field Catalog is an essential tool for personalizing and customizing ALV reports in SAP ABAP. By integrating subtotals and totals into the report, users can provide additional context and insight into the data being presented, leading to a more efficient and effective user experience.

Sample Report for Reference - 

REPORT  ZAVI_FEVER2.

TYPE-POOLS: SLIS.

DATA: IT_SPFLI TYPE TABLE OF SPFLI,
      WA_SPFLI TYPE SPFLI.
DATA: IT_FCAT TYPE  SLIS_T_FIELDCAT_ALV,
      WA_FCAT TYPE  SLIS_FIELDCAT_ALV.
DATA: IT_sort TYPE slis_t_sortinfo_alv.
DATA: WA_LAYOUT TYPE SLIS_LAYOUT_ALV.
DATA: WA_sort TYPE slis_SORTinfo_alv.
SELECT * FROM SPFLI INTO TABLE IT_SPFLI .

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
   EXPORTING
     I_PROGRAM_NAME               = 'SY-REPID'
*     I_INTERNAL_TABNAME           =
     I_STRUCTURE_NAME             = 'SPFLI'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_INCLNAME                   =
*     I_BYPASSING_BUFFER           =
*     I_BUFFER_ACTIVE              =
    CHANGING
      ct_fieldcat                  = IT_FCAT
   EXCEPTIONS
     INCONSISTENT_INTERFACE       = 1
     PROGRAM_ERROR                = 2
     OTHERS                       = 3
            .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

WA_SORT-FIELDNAME = 'CARRID'.
WA_SORT-SPOS = '1'.
WA_SORT-UP = 'X'.
WA_SORT-SUBTOT = 'X'.
APPEND WA_SORT TO IT_SORT.


LOOP AT IT_FCAT INTO WA_FCAT.
  IF WA_FCAT-FIELDNAME = 'DISTANCE'.
    WA_FCAT-DO_SUM = 'X'.
    MODIFY IT_FCAT FROM WA_FCAT TRANSPORTING DO_SUM.
    ENDIF.
    ENDLOOP.

    WA_LAYOUT-TOTALS_TEXT = 'TOTAL'.

 CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
  EXPORTING
*    I_INTERFACE_CHECK              = ' '
*    I_BYPASSING_BUFFER             =
*    I_BUFFER_ACTIVE                = ' '
    I_CALLBACK_PROGRAM             = 'SY-REPID'
*    I_CALLBACK_PF_STATUS_SET       = ' '
*    I_CALLBACK_USER_COMMAND        = ' '
    I_STRUCTURE_NAME               = 'SPFLI'
    IS_LAYOUT                      = WA_LAYOUT
    IT_FIELDCAT                    = IT_FCAT
*    IT_EXCLUDING                   =
*    IT_SPECIAL_GROUPS              =
    IT_SORT                        = IT_SORT
*    IT_FILTER                      =
*    IS_SEL_HIDE                    =
*    I_DEFAULT                      = 'X'
*    I_SAVE                         = ' '
*    IS_VARIANT                     =
*    IT_EVENTS                      =
*    IT_EVENT_EXIT                  =
*    IS_PRINT                       =
*    IS_REPREP_ID                   =
*    I_SCREEN_START_COLUMN          = 0
*    I_SCREEN_START_LINE            = 0
*    I_SCREEN_END_COLUMN            = 0
*    I_SCREEN_END_LINE              = 0
*    IR_SALV_LIST_ADAPTER           =
*    IT_EXCEPT_QINFO                =
*    I_SUPPRESS_EMPTY_DATA          = ABAP_FALSE
*  IMPORTING
*    E_EXIT_CAUSED_BY_CALLER        =
*    ES_EXIT_CAUSED_BY_USER         =
   TABLES
     t_outtab                       = IT_SPFLI
  EXCEPTIONS
    PROGRAM_ERROR                  = 1
    OTHERS                         = 2
           .
 IF sy-subrc <> 0.
* Implement suitable error handling here
 ENDIF

.