✒️ABAP El ALV jerárquico y el agrupamiento
ABAP El ALV jerárquico y el agrupamiento
Grouping in an ALV Report
We can group the records displayed in the ALV so that multiple records with the same value for a particular field are displayed in a grouped manner by that field. To achieve that we should execute the following steps:
- Declare the internal table TI_SORT of type SLIS_T_SORTINFO_ALV and the structure WA_SORT of type SLIS_SORTINFO_ALV.
DATA: ti_sort TYPE slis_t_sortinfo_alv,
wa_sort TYPE slis_sortinfo_alv.
- Next, within the START-OF-SELECTION event and before calling the function module that executes the ALV, declare the subroutine GROUP_FIELDS as follows:
FORM group_fields.
REFRESH ti_sort.
CLEAR wa_sort.
wa_sort-spos = 1. "sorting order
wa_sort-fieldname = 'USER_STATUS'. "sorting field
APPEND wa_sort TO ti_sort.
ENDFORM.
- We can group an ALV report by all fields of the table if needed. Simply add the fields to be grouped in the internal table TI_SORT.
- Now, in the declaration of the function module 'REUSE_ALV_GRID_DISPLAY', complete the EXPORTING IT_SORT parameter with our internal table TI_SORT.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = v_repid
i_callback_pf_status_set = 'PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
it_fieldcat = ti_catalogo[]
i_callback_top_of_page = 'TOP_OF_PAGE'
is_layout = wa_layout
it_sort = ti_sort " Sorting instructions
TABLES
t_outtab = ti_users
EXCEPTIONS
program_error = 1
OTHERS = 2.
- Repeat the same for the 'REUSE_ALV_LIST_DISPLAY' function module for the ALV LIST report.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = v_repid
it_fieldcat = ti_catalogo[]
is_layout = wa_layout
it_sort = ti_sort " sorting instructions
TABLES
t_outtab = ti_usuarios
EXCEPTIONS
program_error = 1
OTHERS = 2.
It can be observed that when executing the ALV GRID report, grouping is done by USER_STATUS, and the same statement applies to the ALV LIST function.
Conclusion: Graphical Grouping by columns in the ALV report is available for ALV GRID and NOT for ALV LIST.
Hierarchical ALV
Hierarchical ALVs are used with header and detail or position, and there must be a common field. Here is a step by step example of creating a hierarchical ALV. (Company and flight information for each company)
- Declare internal table TI_HEADER, containing short names (CARRID) and names (CARRNAME) of airlines, and TI_DETAIL with fields related to flights.
DATA: BEGIN OF ti_header OCCURS 0,
carrid LIKE scarr-carrid,
carrname LIKE scarr-carrname,
END OF ti_header.
DATA: BEGIN OF ti_detail OCCURS 0,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-price,
price LIKE sflight-price,
currency LIKE sflight-currency,
planetype LIKE sflight-planetype,
END OF ti_detail.
- Given that we will generate the catalog for the hierarchical ALV automatically, use the OCCURS statement for both internal tables.
- Next, declare a structure of type SLIS_KEYINFO_ALV.
DATA: wa_keyinfo TYPE slis_keyinfo_alv.
- In the START-OF-SELECTION event, declare the subroutine LOAD_DATA.
* Load data into internal tables
PERFORM load_data.
- Inside the LOAD_DATA subroutine, populate the TI_HEADER and TI_DETAIL internal tables with data for the ARG airline.
FORM load_data .
REFRESH: ti_header, ti_detail.
* Header 1
CLEAR ti_header.
ti_header-carrid = 'ARG'.
ti_header-carrname = 'Aerolineas Argentina'.
APPEND ti_header.
* Detail 1
CLEAR ti_detail.
ti_detail-carrid = 'ARG'.
ti_detail-connid = '1010'.
ti_detail-fldate = '20091111'.
ti_detail-price = '380'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A310'.
APPEND ti_detail.
* Detail 2
CLEAR ti_detail.
ti_detail-carrid = 'ARG'.
ti_detail-connid = '1020'.
ti_detail-fldate = '20091011'.
ti_detail-price = '300'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.ENDFORM.
- In the START-OF-SELECTION event, declare the subroutine CONFIGURE_LAYOUT.
* Configure layout
PERFORM configure_layout.
- Inside the CONFIGURE_LAYOUT subroutine, configure the layout settings.
*&---------------------------------------------------------------------*
*& Form CONFIGURE_LAYOUT
*&---------------------------------------------------------------------*
FORM configure_layout .
CLEAR wa_layout.
wa_layout-zebra = c_x. " Line striping
wa_layout-window_titlebar = TEXT-001. " Flight Report
ENDFORM. " CONFIGURE_LAYOUT
- Create the ALV catalog for both tables using the build_auto_catalog subroutine.
* Build catalog
PERFORM build_auto_catalog.
- Inside this subroutine, execute the function module REUSE_ALV_FIELDCATALOG_MERGE to generate the catalog automatically by combining the fields of both internal tables in the TI_CATALOG internal table.
*&---------------------------------------------------------------------*
*& Form BUILD_AUTO_CATALOG
*&---------------------------------------------------------------------*
FORM build_auto_catalog .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'TI_HEADER'
i_client_never_display = c_x
i_inclname = sy-repid
CHANGING
ct_fieldcat = ti_catalog[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'TI_DETAIL'
i_client_never_display = c_x
i_inclname = sy-repid
CHANGING
ct_fieldcat = ti_catalog[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " BUILD_AUTO_CATALOG
Note: In a hierarchical ALV, the TOP-OF-PAGE event cannot be used, preventing the generation of a header with titles and logos as done in GRID or LIST ALVs. Another limitation is the inability to use the data export to Excel due to the format of the header and detail for each record.
E.g
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_19
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_19.
* Types
TYPE-POOLS: slis.
* Constants
CONSTANTS: c_x(1) TYPE c VALUE 'X'.
* Internal table for header
DATA: BEGIN OF ti_header OCCURS 0,
carrid LIKE scarr-carrid,
carrname LIKE scarr-carrname,
END OF ti_header.
* Internal table for details
DATA: BEGIN OF ti_detail OCCURS 0,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
price LIKE sflight-price,
currency LIKE sflight-currency,
planetype LIKE sflight-planetype,
END OF ti_detail.
DATA: ti_catalog TYPE slis_t_fieldcat_alv,
wa_catalog TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv,
wa_keyinfo TYPE slis_keyinfo_alv.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
* Load data into internal tables
PERFORM load_data.
* Configure layout
PERFORM configure_layout.
* Build catalog
PERFORM build_auto_catalog.
* Determine Header-Item association
PERFORM determine_association.
* Execute ALV
PERFORM execute_hierarchical_alv.
*&---------------------------------------------------------------------*
*& Form LOAD_DATA
*&---------------------------------------------------------------------*
FORM load_data .
REFRESH: ti_header, ti_detail.
* Header 1
CLEAR ti_header.
ti_header-carrid = 'ARG'.
ti_header-carrname = 'Aerolineas Argentina'.
APPEND ti_header.
* Detail 1
CLEAR ti_detail.
ti_detail-carrid = 'ARG'.
ti_detail-connid = '1010'.
ti_detail-fldate = '20091111'.
ti_detail-price = '380'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A310'.
APPEND ti_detail.
* Detail 2
CLEAR ti_detail.
ti_detail-carrid = 'ARG'.
ti_detail-connid = '1020'.
ti_detail-fldate = '20091011'.
ti_detail-price = '300'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
* Detail 3
CLEAR ti_detail.
ti_detail-carrid = 'ARG'.
ti_detail-connid = '1080'.
ti_detail-fldate = '20210516'.
ti_detail-price = '1300'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
* Detail 4
CLEAR ti_detail.
ti_detail-carrid = 'ARG'.
ti_detail-connid = '1090'.
ti_detail-fldate = '20220808'.
ti_detail-price = '1500'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
* Header 2
CLEAR ti_header.
ti_header-carrid = 'LAN'.
ti_header-carrname = 'Lan Chile'.
APPEND ti_header.
* Detail 1
CLEAR ti_detail.
ti_detail-carrid = 'LAN'.
ti_detail-connid = '1030'.
ti_detail-fldate = '20220603'.
ti_detail-price = '500'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
* Detail 2
CLEAR ti_detail.
ti_detail-carrid = 'LAN'.
ti_detail-connid = '1040'.
ti_detail-fldate = '20210101'.
ti_detail-price = '600'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
* Detail 3
CLEAR ti_detail.
ti_detail-carrid = 'LAN'.
ti_detail-connid = '1050'.
ti_detail-fldate = '20211007'.
ti_detail-price = '999'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
* Detail 4
CLEAR ti_detail.
ti_detail-carrid = 'LAN'.
ti_detail-connid = '1050'.
ti_detail-fldate = '20230215'.
ti_detail-price = '6000'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
* Detail 5
CLEAR ti_detail.
ti_detail-carrid = 'LAN'.
ti_detail-connid = '1060'.
ti_detail-fldate = '20211231'.
ti_detail-price = '1000'.
ti_detail-currency = 'USD'.
ti_detail-planetype = 'A330'.
APPEND ti_detail.
ENDFORM. " LOAD_DATA
*&---------------------------------------------------------------------*
*& Form BUILD_AUTO_CATALOG
*&---------------------------------------------------------------------*
FORM build_auto_catalog .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'TI_HEADER'
i_client_never_display = c_x
i_inclname = sy-repid
CHANGING
ct_fieldcat = ti_catalog[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'TI_DETAIL'
i_client_never_display = c_x
i_inclname = sy-repid
CHANGING
ct_fieldcat = ti_catalog[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " BUILD_AUTO_CATALOG
*&---------------------------------------------------------------------*
*& Form DETERMINE_ASSOCIATION
*&---------------------------------------------------------------------*
FORM determine_association .
CLEAR wa_keyinfo.
wa_keyinfo-header01 = 'CARRID'.
wa_keyinfo-item01 = 'CARRID'.
ENDFORM. " DETERMINE_ASSOCIATION
*&---------------------------------------------------------------------*
*& Form EXECUTE_HIERARCHICAL_ALV
*&---------------------------------------------------------------------*
FORM execute_hierarchical_alv .
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = ti_catalog[]
i_tabname_header = 'TI_HEADER'
i_tabname_item = 'TI_DETAIL'
is_keyinfo = wa_keyinfo
TABLES
t_outtab_header = ti_header
t_outtab_item = ti_detail
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " EXECUTE_HIERARCHICAL_ALV
*&---------------------------------------------------------------------*
*& Form CONFIGURE_LAYOUT
*&---------------------------------------------------------------------*
FORM configure_layout .
CLEAR wa_layout.
wa_layout-zebra = c_x. " Line striping
wa_layout-window_titlebar = TEXT-001. " Flight Report
ENDFORM. " CONFIGURE_LAYOUT
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computaci?n - Espa?a - Legajo: SW34C
✒️Autor de: 149 Publicaciones Académicas
🎓Egresado de los módulos:
- Carrera Consultor en SAP Fiori
- Carrera Consultor ABAP Nivel Avanzado
- Carrera Consultor ABAP Nivel Inicial
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computaci?n con 8 a?os de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez