Totals and Sub Totals in ALV with IDA on HANA

0
9147

Hello everyone, in this tutorial you will learn how to do totals and subtotals in ALV with IDA on HANA.

Prerequisites

Totals and Sub-totals

To set totals and sub-totals, use the method SET_AGGREGATION( ) in IF_SALV_GUI_LAYOUT_IDA in ABAP ALV with IDA

Totals

Totals in ABAP ALV with IDA can be achieved by using the method SET_AGGREGATION( ). Using this method you can perform following aggregations

  • Sum
  • Maximum
  • Minimum
  • Average

ABAP Source code

START-OF-SELECTION.

*1.. Create object reference for ALV with IDA
*...  based on CDS View
DATA(lo_alv_display) = cl_salv_gui_table_ida=>create_for_cds_view( 
                                   iv_cds_view_name = 'zcds_total_stock' ).

*2.. Create Aggregation rule
DATA: ls_aggr_rule  TYPE if_salv_gui_types_ida=>ys_aggregation_rule,
      lt_aggr_rules TYPE if_salv_gui_types_ida=>yt_aggregation_rule.

ls_aggr_rule-field_name = 'TOTAL_STOCK'.
ls_aggr_rule-function   = if_salv_service_types=>cs_function_code-sum.
APPEND ls_aggr_rule TO lt_aggr_rules.

lo_alv_display->default_layout( )->set_aggregations( lt_aggr_rules ).

*3... Display ALV
  lo_alv_display->fullscreen( )->display( ).

 Output

alv_ida_totals

Sub Totals

Sub Totals in ALV with IDA can be achieved by using both Sorting Rule and Aggregation Rule.

To define a subtotal on a particular column first you need to define sorting rule by using the SET_SORT_ORDER( ) and then apply aggregation rule by using method SET_AGGREGATION( ).

ABAP Source Code

*******************************************************
*                www.saplearners.com                   *
*            Totals & Subtotals in ABAP ALV with IDA   *
********************************************************

START-OF-SELECTION.

*1.. Create object reference for ALV with IDA
*...  based on CDS View
DATA(lo_alv_display) = cl_salv_gui_table_ida=>create_for_cds_view( 
                                  iv_cds_view_name = 'zcds_total_stock' ).


*2.. Create Aggregation
DATA: ls_aggr_rule  TYPE if_salv_gui_types_ida=>ys_aggregation_rule,
      lt_aggr_rules TYPE if_salv_gui_types_ida=>yt_aggregation_rule.

ls_aggr_rule-field_name = 'TOTAL_STOCK'.
ls_aggr_rule-function   = if_salv_service_types=>cs_function_code-sum.
APPEND ls_aggr_rule TO lt_aggr_rules.

lo_alv_display->default_layout( )->set_aggregations( lt_aggr_rules ).


*3.. Set Sorting rule for Subtotals
DATA(lt_sort_rule) = VALUE if_salv_gui_types_ida=>yt_sort_rule(
                           ( field_name = 'CATEGORY'
                             descending = abap_false
                             is_grouped = abap_true )
                           ).
lo_alv_display->default_layout( )->set_sort_order( 
                                   it_sort_order = lt_sort_rule ).

*3... Display ALV
  lo_alv_display->fullscreen( )->display( ).

 Output

alv_ida_sub_totals

Congrats! You have successfully learned how to perform totals and subtotals in ALV with IDA on HANA.

Please stay tuned for ABAP ALV with IDA on HANA tutorials. Leave a comment in the below comment section and let us know your feedback.

Like us on Facebook and follow us on Twitter.

mail

SAPLearners.com is now on Telegram. Click here to join our channel and stay updated with the latest tutorials and updates.