Hierarchical ALV Using Factory Method in SAP ABAP

Estimated read time 6 min read

In this blog post, we’ll explore how to implement a Hierarchical ALV using the Factory Method of the CL_SALV_HIERSEQ_TABLE class.

Introduction

In many SAP ERP scenarios, business users frequently work with data that follows a natural master–detail relationship, such as customers and their contacts, vendors and their company-code assignments, or materials and their storage locations. Presenting such related datasets in a clean and intuitive format is critical for efficient decision-making.

While traditional ALV is highly effective for displaying single-level tables, it becomes less practical when dealing with multi-level or dependent datasets. For these situations, SAP provides Hierarchical ALV through the class CL_SALV_HIERSEQ_TABLE, allowing developers to present complex data relationships in an expandable, easy-to-navigate layout within SAP GUI.

This blog demonstrates how to create a hierarchical ALV using a vendor master table and its corresponding item details. The objective is to show how straightforward it is to bind two related datasets, define the hierarchy mapping, and display the result using SAP’s SALV framework—all with clean, maintainable ABAP code.

Scenario Overview

We will work with two custom tables:

ZVA_VENDOR_TA – Stores vendor master information (header level).

ZVA_VENDOR_ITEM – Stores vendor-specific or company-code-specific details (item level).

The goal is to display these tables in a hierarchical ALV, where each vendor (header) can be expanded to reveal its associated item entries (detail).

CODE:

REPORT ZVA_FACTORY_HIERACY_ALV.

” Declaration of internal tables and work areas for hierarchical ALV binding
DATA: LT_HIERSEQ TYPE SALV_T_HIERSEQ_BINDING,
LS_HIERSEQ TYPE SALV_S_HIERSEQ_BINDING.

” Select vendor master data from custom table ZVA_VENDOR_TA
SELECT VENDERNO, NAME, CITY, POSTALCODE, EMAIL, ACCOUNTGROUP, PROCESSORGROUP, POSTINGBLOCK
FROM ZVA_VENDOR_TA INTO TABLE (LT_VENDOR).

” Check if data is available in the LT_VENDOR table
IF sy-subrc = 0.

” If vendor data exists, select corresponding vendor item details from ZVA_VENDOR_ITEM
SELECT VENDERNO, COMPANYCODE, PERSONNELNUMBER, AUTHORGROUP, NTGEW, GEWEI, EKORG, STATUS
FROM ZVA_VENDOR_ITEM INTO TABLE (LT_VENDOR_ITEM).

ENDIF.

” Define the hierarchy relationship between the master and detail tables
LS_HIERSEQ-MASTER = ‘VENDERNO’. ” Master key field (header level)
LS_HIERSEQ-SLAVE = ‘VENDERNO’. ” Corresponding key field in detail (item level)
APPEND LS_HIERSEQ TO LT_HIERSEQ. ” Add the mapping to the hierarchy sequence table

” Create and display the hierarchical ALV using CL_SALV_HIERSEQ_TABLE
CALL METHOD CL_SALV_HIERSEQ_TABLE=>FACTORY
EXPORTING
T_BINDING_LEVEL1_LEVEL2 = LT_HIERSEQ ” Define the binding between header and item tables
IMPORTING
R_HIERSEQ = DATA(LO_SALV_HIERSEQ) ” Get reference to the hierarchical ALV object
CHANGING
T_TABLE_LEVEL1 = LT_VENDOR ” Pass header (vendor) data
T_TABLE_LEVEL2 = LT_VENDOR_ITEM. ” Pass item (vendor details) data

” Display the hierarchical ALV
LO_SALV_HIERSEQ->DISPLAY( ).

Output :

Conclusion

Hierarchical ALV is a powerful tool for presenting master–detail datasets in an organized and interactive format. The class CL_SALV_HIERSEQ_TABLE significantly simplifies the development process by handling the hierarchy logic internally, allowing developers to focus on data retrieval and business requirements rather than UI complexity.

By implementing this approach, organizations can deliver reports that are not only visually clear but also easier for end users to navigate—especially when dealing with multi-level relational data. This technique remains highly relevant for SAP GUI-based reporting and continues to be an important part of the ABAP developer’s toolkit.

If needed, this solution can be extended to support additional hierarchy levels, custom toolbar functions, or integration with business logic, making it flexible enough for advanced reporting scenarios.

 

 

 

​ In this blog post, we’ll explore how to implement a Hierarchical ALV using the Factory Method of the CL_SALV_HIERSEQ_TABLE class.IntroductionIn many SAP ERP scenarios, business users frequently work with data that follows a natural master–detail relationship, such as customers and their contacts, vendors and their company-code assignments, or materials and their storage locations. Presenting such related datasets in a clean and intuitive format is critical for efficient decision-making.While traditional ALV is highly effective for displaying single-level tables, it becomes less practical when dealing with multi-level or dependent datasets. For these situations, SAP provides Hierarchical ALV through the class CL_SALV_HIERSEQ_TABLE, allowing developers to present complex data relationships in an expandable, easy-to-navigate layout within SAP GUI.This blog demonstrates how to create a hierarchical ALV using a vendor master table and its corresponding item details. The objective is to show how straightforward it is to bind two related datasets, define the hierarchy mapping, and display the result using SAP’s SALV framework—all with clean, maintainable ABAP code.Scenario OverviewWe will work with two custom tables:ZVA_VENDOR_TA – Stores vendor master information (header level).ZVA_VENDOR_ITEM – Stores vendor-specific or company-code-specific details (item level).The goal is to display these tables in a hierarchical ALV, where each vendor (header) can be expanded to reveal its associated item entries (detail).CODE:REPORT ZVA_FACTORY_HIERACY_ALV.

” Declaration of internal tables and work areas for hierarchical ALV binding
DATA: LT_HIERSEQ TYPE SALV_T_HIERSEQ_BINDING,
LS_HIERSEQ TYPE SALV_S_HIERSEQ_BINDING.

” Select vendor master data from custom table ZVA_VENDOR_TA
SELECT VENDERNO, NAME, CITY, POSTALCODE, EMAIL, ACCOUNTGROUP, PROCESSORGROUP, POSTINGBLOCK
FROM ZVA_VENDOR_TA INTO TABLE (LT_VENDOR).

” Check if data is available in the LT_VENDOR table
IF sy-subrc = 0.

” If vendor data exists, select corresponding vendor item details from ZVA_VENDOR_ITEM
SELECT VENDERNO, COMPANYCODE, PERSONNELNUMBER, AUTHORGROUP, NTGEW, GEWEI, EKORG, STATUS
FROM ZVA_VENDOR_ITEM INTO TABLE (LT_VENDOR_ITEM).

ENDIF.

” Define the hierarchy relationship between the master and detail tables
LS_HIERSEQ-MASTER = ‘VENDERNO’. ” Master key field (header level)
LS_HIERSEQ-SLAVE = ‘VENDERNO’. ” Corresponding key field in detail (item level)
APPEND LS_HIERSEQ TO LT_HIERSEQ. ” Add the mapping to the hierarchy sequence table

” Create and display the hierarchical ALV using CL_SALV_HIERSEQ_TABLE
CALL METHOD CL_SALV_HIERSEQ_TABLE=>FACTORY
EXPORTING
T_BINDING_LEVEL1_LEVEL2 = LT_HIERSEQ ” Define the binding between header and item tables
IMPORTING
R_HIERSEQ = DATA(LO_SALV_HIERSEQ) ” Get reference to the hierarchical ALV object
CHANGING
T_TABLE_LEVEL1 = LT_VENDOR ” Pass header (vendor) data
T_TABLE_LEVEL2 = LT_VENDOR_ITEM. ” Pass item (vendor details) data

” Display the hierarchical ALV
LO_SALV_HIERSEQ->DISPLAY( ).Output :ConclusionHierarchical ALV is a powerful tool for presenting master–detail datasets in an organized and interactive format. The class CL_SALV_HIERSEQ_TABLE significantly simplifies the development process by handling the hierarchy logic internally, allowing developers to focus on data retrieval and business requirements rather than UI complexity.By implementing this approach, organizations can deliver reports that are not only visually clear but also easier for end users to navigate—especially when dealing with multi-level relational data. This technique remains highly relevant for SAP GUI-based reporting and continues to be an important part of the ABAP developer’s toolkit.If needed, this solution can be extended to support additional hierarchy levels, custom toolbar functions, or integration with business logic, making it flexible enough for advanced reporting scenarios.     Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author