Friday, July 11, 2008

Adding subtotals at the end of an ALV List

By Joyjit Ghosh,
Kolkata
, India
.


Introduction:

The normal ALV list functionality provides for subtotals within the list and one grand total at the end of the report. If it is required to display the subtotals not within the list, but at the end of report the standard ALV functionality does not provide any way to do this. The tip described here caters to this special requirement.

Normal Functionality (provided for in the standard ALV)

Item Heading

Item 1.1

Item 1.2

Item 1.3

Subtotal 1

Item 2.1

Item 2.2

Subtotal 2

Total


Special Requirement (can be achieved using this tip)

Item Heading

Item 1.1

Item 1.2

Item 1.3

Item 2.1

Item 2.2

Subtotal 1 (Success)

Subtotal 2 (Error)


Detail Steps:

Step 1

Define an internal table of type SLIS_T_EVENT, and a work area of type SLIS_ALV_EVENT.

DATA: T_EVENT TYPE SLIS_T_EVENT,

W_EVENT TYPE SLIS_ALV_EVENT.

Step 2

Append AFTER-LINE-OUTPUT event to the internal table T_EVENT.

CLEAR W_EVENT.

W_EVENT-FORM = SLIS_EV_AFTER_LINE_OUTPUT.

W_EVENT-NAME = SLIS_EV_AFTER_LINE_OUTPUT.“AFTER_LINE_OUTPUT event

APPEND W_EVENT TO T_EVENT.


Step 3

The subtotals are calculated and displayed in the list from the subroutine AFTER_LINE_OUTPUT, which corresponds to the event AFTER_LINE_OUTPUT. This subroutine uses the parameter P_RS_LINEINFO, which contains settings for each line displayed in the list. The actual subtotal’s output is created using the WRITE and FORMAT statements.

FORM AFTER_LINE_OUTPUT

USING P_RS_LINEINFO TYPE SLIS_LINEINFO.

* Declaration of local variables

DATA: L_SUCCESS TYPE WRBTR, "Total For Successful Entries

L_ERROR TYPE WRBTR, "Total For Unsuccessful Entries

L_COUNT TYPE I. "No. Of lines in table T_OUTPUT

* Getting No. of Lines in the table T_OUTPUT

DESCRIBE TABLE T_OUTPUT LINES L_COUNT.

* Displaying the totals after the last record of the internal

* table T_OUTPUT

IF P_RS_LINEINFO-TABINDEX = L_COUNT.

* Loop At the internal table T_OUTPUT

LOOP AT T_OUTPUT INTO W_OUTPUT.

IF W_OUTPUT-SFLAG = C_CHECKED.

"Flag: Indicates error record

* Calculate total for unsuccessful entries

L_ERROR = L_ERROR + W_OUTPUT-WRBTR.

ELSE.

* Calculate total for successful entries

L_SUCCESS = L_SUCCESS + W_OUTPUT-WRBTR.

ENDIF.

* Clear workarea W_OUTPUT

CLEAR W_OUTPUT.

ENDLOOP.

* Set format for the total line display

ULINE AT (P_RS_LINEINFO-LINSZ). "Dynamic Line Size

FORMAT INTENSIFIED COLOR COL_TOTAL ON. "Setting the color

"For the total row

"As Yellow

WRITE : / SY-VLINE, "Vertical Line

TEXT-017, "Caption For Total

"Sum of Successful

"Entries

33 L_SUCCESS, "Total Of Successful

"Entries

C_USD. "Currency Type USD

POSITION P_RS_LINEINFO-LINSZ. "Dynamic Line Size

WRITE : SY-VLINE. "Vertical Line

ULINE AT (P_RS_LINEINFO-LINSZ). "Dynamic Line Size

WRITE : / SY-VLINE , "Vertical Line

TEXT-018, "Caption For Total

"Sum of Successful

"Entries

33 L_ERROR, "Total Of Unsuccessful

"Entries

C_USD. "Currency Type USD

POSITION P_RS_LINEINFO-LINSZ. "Dynamic Line Size

WRITE : SY-VLINE. "Vertical Line

FORMAT COLOR OFF. "Color Setting Off

ENDIF.

ENDFORM. "AFTER_LINE_OUTPUT

Step 4

The table T_EVENT is passed to the function 'REUSE_ALV_LIST_DISPLAY' while displaying the ALV Report.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = L_REPID "Program Name

IS_LAYOUT = W_LAYOUT "Layout of the Report

IT_FIELDCAT = T_FIELDCAT "Field Catalog for Report

IT_EVENTS = T_EVENT "For setting the events

TABLES

T_OUTTAB = T_OUTPUT "Report data Internal Table

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

Result:

The rows ‘Sum of Successful Entries’ and ‘Sum of Unsuccessful Entries’, showing total of all the successful (green) entries and total of all the unsuccessful (red) entries respectively, have been added using this method.




No comments: